System.Net.WebClient.UploadFile Method

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

Syntax

public byte[] UploadFile (string address, string fileName)

Parameters

address
The URI of the resource to receive the file. For example, ftp://localhost/samplefile.txt.
fileName
The file to send to the resource. For example, "samplefile.txt".

Returns

A byte array containing the body of the response from the resource.

Exceptions

TypeReason
System.Net.WebException

The absolute URI is not valid.

-or-

filename is null or string.Empty or contains invalid characters, or the specified path to the file does not exist.

-or-

An error occurred while opening the stream or uploading the file.

-or-

There was no response from the server hosting the resource.

-or-

The Content-Type header begins with "multipart".

System.Security.SecurityExceptionLocal file access has not been granted.

Remarks

The WebClient.UploadFile(string, string) method sends a local file to a resource. This method uses the STOR command to upload an FTP resource. For an HTTP resource, the POST method is used.

This method blocks while uploading the file. To continue executing while waiting for the server's response, use one of the erload:System.Net.WebClient.UploadFileAsync methods.

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

If the WebClient.BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not an empty string, it is appended to address.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.

Permissions

Requirements

Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0