Export (0) Print
Expand All

WebClient.UploadFile Method (String, String, String)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Function UploadFile( _
   ByVal address As String, _
   ByVal method As String, _
   ByVal fileName As String _
) As Byte()
[C#]
public byte[] UploadFile(
 string address,
 string method,
 string fileName
);
[C++]
public: unsigned char UploadFile(
 String* address,
 String* method,
 String* fileName
)  __gc[];
[JScript]
public function UploadFile(
   address : String,
 method : 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 request sent with the method method, such as a script or ASP page.
method
The method verb used to send the file to the resource.
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 using the method verb specified in the method parameter and returns any response from the server.

If the method parameter specifies a verb that is not understood by the server, 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 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.

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:
© 2014 Microsoft