WebClient.UploadFile 方法

定义

将本地文件上载到具有指定 URI 的资源。

重载

UploadFile(String, String)

将指定的本地文件上载到具有指定 URI 的资源。

UploadFile(Uri, String)

将指定的本地文件上载到具有指定 URI 的资源。

UploadFile(String, String, String)

使用指定的方法将指定的本地文件上载到指定的资源。

UploadFile(Uri, String, String)

使用指定的方法将指定的本地文件上载到指定的资源。

UploadFile(String, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

将指定的本地文件上载到具有指定 URI 的资源。

public:
 cli::array <System::Byte> ^ UploadFile(System::String ^ address, System::String ^ fileName);
public byte[] UploadFile (string address, string fileName);
member this.UploadFile : string * string -> byte[]
Public Function UploadFile (address As String, fileName As String) As Byte()

参数

address
String

接收文件的资源的 URI。 例如,ftp://localhost/samplefile.txt。

fileName
String

要发送到资源的文件。 例如,“samplefile.txt”。

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

- 或 -

fileName 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

fileNamenull、为 Empty、包含无效字符或者不存在。

- 或 -

上载文件时出错。

- 或 -

承载资源的服务器没有响应。

- 或 -

Content-type 标头以 multipart 开头。

示例

下面的代码示例使用 UploadFile将指定的文件上传到指定的 URI。 服务器返回的任何响应都显示在控制台上。

Console::Write( "\nPlease enter the URI to post data to : " );
String^ uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine( "\nPlease enter the fully qualified path of the file to be uploaded to the URI" );
String^ fileName = Console::ReadLine();
Console::WriteLine( "Uploading {0} to {1} ...", fileName, uriString );

// Upload the file to the URI.
// The 'UploadFile(uriString, fileName)' method implicitly uses HTTP POST method.
array<Byte>^responseArray = myWebClient->UploadFile( uriString, fileName );

// Decode and display the response.
Console::WriteLine( "\nResponse Received::The contents of the file uploaded are: \n {0}", 
    System::Text::Encoding::ASCII->GetString( responseArray ) );
Console.Write("\nPlease enter the URI to post data to : ");
String uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URI");
string fileName = Console.ReadLine();
Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);

// Upload the file to the URI.
// The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method.
byte[] responseArray = myWebClient.UploadFile(uriString,fileName);

// Decode and display the response.
Console.WriteLine("\nResponse Received. The contents of the file uploaded are:\n{0}", 
    System.Text.Encoding.ASCII.GetString(responseArray));

Console.Write(ControlChars.Cr + "Please enter the URI to post data to : ")
Dim uriString As String = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()

Console.WriteLine(ControlChars.Cr & _
    "Please enter the fully qualified path of the file to be uploaded to the URI")

Dim fileName As String = Console.ReadLine()
Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString)

' Upload the file to the URI.
' The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method. 
Dim responseArray As Byte() = myWebClient.UploadFile(uriString, fileName)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr & "Response Received. The contents of the file uploaded are: " & _
    ControlChars.Cr & "{0}", System.Text.Encoding.ASCII.GetString(responseArray))

下面的代码示例显示了一个 ASP.NET 页,该页面可以接受已发布的文件,并且适合与 方法一起使用 UploadFile 。 页面必须驻留在 Web 服务器上。 其地址为 方法的 UploadFile 参数提供值address

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
    
    foreach(string f in Request.Files.AllKeys) {
        HttpPostedFile file = Request.Files[f];
        file.SaveAs("c:\\inetpub\\test\\UploadedFiles\\" + file.FileName);
    }	
}

</Script>
<html>
<body>
<p> Upload complete.  </p>
</body>
</html>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="VB" runat=server>
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        
        Dim f As String
        Dim file
        For Each f In Request.Files.AllKeys
            file = Request.Files(f)
            file.SaveAs("c:\inetpub\test\UploadedFiles\" & file.FileName)
        Next f
        
    End Sub

</Script>
<html>
<body>
<p> Upload complete. </p>
</body>
</html>

注解

方法 UploadFile 将本地文件发送到资源。 此方法使用 STOR 命令上传 FTP 资源。 对于 HTTP 资源,使用 POST 方法。

此方法在上传文件时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadFileAsync 一。

方法 POST 由 HTTP 定义。 如果基础请求不使用 HTTP 且 POST 服务器无法理解,则基础协议类将确定所发生的情况。 通常, WebException 会引发 ,并将 Status 属性设置为指示错误。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

UploadFile(Uri, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

将指定的本地文件上载到具有指定 URI 的资源。

public:
 cli::array <System::Byte> ^ UploadFile(Uri ^ address, System::String ^ fileName);
public byte[] UploadFile (Uri address, string fileName);
member this.UploadFile : Uri * string -> byte[]
Public Function UploadFile (address As Uri, fileName As String) As Byte()

参数

address
Uri

接收文件的资源的 URI。 例如,ftp://localhost/samplefile.txt。

fileName
String

要发送到资源的文件。 例如,“samplefile.txt”。

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

- 或 -

fileName 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

fileNamenull、为 Empty、包含无效字符或者不存在。

- 或 -

上载文件时出错。

- 或 -

承载资源的服务器没有响应。

- 或 -

Content-type 标头以 multipart 开头。

注解

方法 UploadFile 将本地文件发送到资源。 此方法使用 STOR 命令上传 FTP 资源。 对于 HTTP 资源,使用 POST 方法。

此方法在上传文件时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadFileAsync 一。

方法 POST 由 HTTP 定义。 如果基础请求不使用 HTTP 且 POST 服务器无法理解,则基础协议类将确定所发生的情况。 通常, WebException 会引发 ,并将 Status 属性设置为指示错误。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

UploadFile(String, String, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

使用指定的方法将指定的本地文件上载到指定的资源。

public:
 cli::array <System::Byte> ^ UploadFile(System::String ^ address, System::String ^ method, System::String ^ fileName);
public byte[] UploadFile (string address, string? method, string fileName);
public byte[] UploadFile (string address, string method, string fileName);
member this.UploadFile : string * string * string -> byte[]
Public Function UploadFile (address As String, method As String, fileName As String) As Byte()

参数

address
String

接收文件的资源的 URI。

method
String

用于将文件发送到资源的方法。 如果为 null,则对于 http 默认值为 POST,对于 ftp 默认值为 STOR。

fileName
String

要发送到资源的文件。

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

- 或 -

fileName 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

fileNamenull、为 Empty、包含无效字符或者不存在。

- 或 -

上载文件时出错。

- 或 -

承载资源的服务器没有响应。

- 或 -

Content-type 标头以 multipart 开头。

示例

下面的代码示例使用 UploadFile将指定的文件上传到指定的 URI。 服务器返回的任何响应都显示在控制台上。

Console::Write( "\nPlease enter the URL to post data to : " );
String^ uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine
    ("\nPlease enter the fully qualified path of the file to be uploaded to the URL" );
String^ fileName = Console::ReadLine();
Console::WriteLine( "Uploading {0} to {1} ...", fileName, uriString );

// Upload the file to the URL using the HTTP 1.0 POST.
array<Byte>^responseArray = myWebClient->UploadFile( uriString, "POST", fileName );

// Decode and display the response.
Console::WriteLine( "\nResponse Received::The contents of the file uploaded are: \n {0}", 
    System::Text::Encoding::ASCII->GetString( responseArray ));
Console.Write("\nPlease enter the URL to post data to : ");
String uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URL");
string fileName = Console.ReadLine();

Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);						
// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);

// Decode and display the response.
Console.WriteLine("\nResponse Received. The contents of the file uploaded are:\n{0}",
    System.Text.Encoding.ASCII.GetString(responseArray));


Console.Write(ControlChars.Cr + "Please enter the URL to post data to : ")
Dim uriString As String = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()
Console.WriteLine(ControlChars.Cr & _
    "Please enter the fully qualified path of the file to be uploaded to the URL")

Dim fileName As String = Console.ReadLine()
Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString)

' Upload the file to the Url using the HTTP 1.0 POST.
Dim responseArray As Byte() = myWebClient.UploadFile(uriString, "POST", fileName)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response Received. The contents of the file uploaded are: " & _
    ControlChars.Cr & "{0}", System.Text.Encoding.ASCII.GetString(responseArray))

下面的代码示例显示了一个 ASP.NET 页,该页面可以接受已发布的文件,并且适合与 方法一起使用 UploadFile 。 页面必须驻留在 Web 服务器上。 其地址为 方法的 UploadFile 参数提供值address

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
    
    foreach(string f in Request.Files.AllKeys) {
        HttpPostedFile file = Request.Files[f];
        file.SaveAs("c:\\inetpub\\test\\UploadedFiles\\" + file.FileName);
    }	
}

</Script>
<html>
<body>
<p> Upload complete.  </p>
</body>
</html>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="VB" runat=server>
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        
        Dim f As String
        Dim file
        For Each f In Request.Files.AllKeys
            file = Request.Files(f)
            file.SaveAs("c:\inetpub\test\UploadedFiles\" & file.FileName)
        Next f
        
    End Sub

</Script>
<html>
<body>
<p> Upload complete. </p>
</body>
</html>

注解

当 address 指定 HTTP 资源时, UploadFile 该方法使用 参数中指定的 HTTP 方法将本地文件发送到资源, method 并从服务器返回任何响应。 此方法在上传文件时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadFileAsync 一。

method如果 参数指定了服务器或资源无法理解的address谓词,则基础协议类将确定所发生的情况。 通常, WebException 会引发 ,并将 Status 属性设置为指示错误。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

UploadFile(Uri, String, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

使用指定的方法将指定的本地文件上载到指定的资源。

public:
 cli::array <System::Byte> ^ UploadFile(Uri ^ address, System::String ^ method, System::String ^ fileName);
public byte[] UploadFile (Uri address, string? method, string fileName);
public byte[] UploadFile (Uri address, string method, string fileName);
member this.UploadFile : Uri * string * string -> byte[]
Public Function UploadFile (address As Uri, method As String, fileName As String) As Byte()

参数

address
Uri

接收文件的资源的 URI。

method
String

用于将文件发送到资源的方法。 如果为 null,则对于 http 默认值为 POST,对于 ftp 默认值为 STOR。

fileName
String

要发送到资源的文件。

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

- 或 -

fileName 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

fileNamenull、为 Empty、包含无效字符或者不存在。

- 或 -

上载文件时出错。

- 或 -

承载资源的服务器没有响应。

- 或 -

Content-type 标头以 multipart 开头。

注解

当 address 指定 HTTP 资源时, UploadFile 该方法使用 参数中指定的 HTTP 方法将本地文件发送到资源, method 并从服务器返回任何响应。 此方法在上传文件时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadFileAsync 一。

method如果 参数指定了服务器或资源无法理解的address谓词,则基础协议类将确定所发生的情况。 通常, WebException 会引发 ,并将 Status 属性设置为指示错误。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于