Export (0) Print
Expand All

WebClient.UploadFile Method

Uploads a local file to a resource with the specified URI.

Overload List

Uploads the specified local file to a resource with the specified URI.

[Visual Basic] Overloads Public Function UploadFile(String, String) As Byte()
[C#] public byte[] UploadFile(string, string);
[C++] public: unsigned char UploadFile(String*, String*) __gc[];
[JScript] public function UploadFile(String, String) : Byte[];

Uploads the specified local file to the specified resource using the specified method.

[Visual Basic] Overloads Public Function UploadFile(String, String, String) As Byte()
[C#] public byte[] UploadFile(string, string, string);
[C++] public: unsigned char UploadFile(String*, String*, String*) __gc[];
[JScript] public function UploadFile(String, String, String) : Byte[];

Example

[Visual Basic, C#, C++] The following example uploads the specified file to the specified URI using UploadFile. Any response returned by the server is displayed on the console.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of UploadFile. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
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}", Encoding.ASCII.GetString(responseArray))

[C#] 
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}",Encoding.ASCII.GetString(responseArray));

[C++] 
Console::Write(S"\nPlease enter the URL to post data to : ");
String*  uriString = Console::ReadLine();

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

Console::WriteLine(S"\nPlease enter the fully qualified path of the file to be uploaded to the URL");
String* fileName = Console::ReadLine();

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

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

[Visual Basic, C#, C++] The following example shows an ASP.NET page that can accept posted files and is suitable for use with the UploadFile method. The page must reside on a Web server. Its address provides the value for the address parameter of the UploadFile method.

[Visual Basic] 
<%@ 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>

[C#] 
<%@ 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>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

WebClient Class | WebClient Members | System.Net Namespace

Show:
© 2014 Microsoft