This documentation is archived and is not being maintained.

WebClient.UploadFile Method (String, String)

.NET Framework 1.1

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

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

Parameters

address
The URI of the resource to receive the file. This URI must identify a resource that can accept a POST request, such as a script or ASP page.
fileName
The file to send to the resource.

Return Value

An array of bytes containing the body of any response from the resource.

Exceptions

Exception Type Condition
WebException The URI formed by combining BaseAddress, and address is invalid.

-or-

fileName is a null reference (Nothing in Visual Basic), Empty, or contains invalid character, or the specified path to the file does not exixts.

-or-

An error occurred while opening the stream.

-or-

There was no response from the server hosting the resource.

-or-

The Content-type header begins with "multipart".

SecurityException Local file access has not been granted.

Remarks

The UploadFile method sends a local file to a resource. The underlying request is made using the POST method verb.

The POST verb is defined by HTTP. If the underlying request does not use HTTP and POST is not understood by the server or by the address resource, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the Status property set to indicate the error.

If the BaseAddress property is not empty, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. If the QueryString property is not empty, it is appended to address.

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

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

[C++] 
Console::Write(S"\nPlease enter the URI 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 URI");
String* fileName = Console::ReadLine();
Console::WriteLine(S"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(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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

WebClient Class | WebClient Members | System.Net Namespace | WebClient.UploadFile Overload List

Show: