.NET API Documentation

System.Net.WebClient.UploadFileAsync

Warning: The WebClient class is considered obsolete and will be removed in a future version. Consider using the HttpClient class.

Summary

Uploads a file to a resource asynchronously.

Syntax

public void UploadFileAsync (Uri address, string filePath); public void UploadFileAsync (Uri address, string method, string filePath); public void UploadFileAsync (string address, string filePath); public void UploadFileAsync (string address, string method, string filePath); public void UploadFileAsync (Uri address, byte[] fileData); public void UploadFileAsync (Uri address, string method, byte[] fileData); public void UploadFileAsync (string address, byte[] fileData); public void UploadFileAsync (string address, string method, byte[] fileData); public void UploadFileAsync (Uri address, string filePath, object userToken); public void UploadFileAsync (Uri address, string method, string filePath, object userToken); public void UploadFileAsync (string address, string filePath, object userToken); public void UploadFileAsync (string address, string method, string filePath, object userToken); public void UploadFileAsync (Uri address, byte[] fileData, object userToken); public void UploadFileAsync (Uri address, string method, byte[] fileData, object userToken); public void UploadFileAsync (string address, byte[] fileData, object userToken); public void UploadFileAsync (string address, string method, byte[] fileData, object userToken);

Parameters

Parameter Description
address The URI to which the file is uploaded.
method The HTTP method used to upload the file. Common values include "POST" or "PUT".
filePath The path to the file to upload.
fileData A byte array containing the file content to upload.
userToken An object that distinguishes this asynchronous request from other requests.

Remarks

The UploadFileAsync method uploads a file to a resource using a WebClient. This method does not block the thread while the file is being uploaded. Use the UploadFileCompleted event to receive notification when the upload operation is complete.

The method parameter can be used to specify the HTTP verb. If not specified, "POST" is used by default when uploading a file.

If you need to upload data directly as a byte array without specifying a file path, use the UploadDataAsync method.

Events

Example

The following example demonstrates how to upload a file asynchronously.


using System;
using System.Net;
using System.ComponentModel;

public class UploadExample
{
    public static void Main(string[] args)
    {
        using (WebClient client = new WebClient())
        {
            // Subscribe to the UploadFileCompleted event
            client.UploadFileCompleted += new UploadFileCompletedEventHandler(client_UploadFileCompleted);

            // Set the URI of the server to upload the file to
            Uri uri = new Uri("http://your-server.com/upload.php");

            // Specify the local file path
            string filePath = @"C:\path\to\your\local\file.txt";

            // Start the asynchronous upload
            client.UploadFileAsync(uri, filePath, "upload_task_1");

            Console.WriteLine("File upload started. Waiting for completion...");
            // Keep the console window open until the upload is complete (or simulated)
            Console.ReadKey();
        }
    }

    // Event handler for UploadFileCompleted
    private static void client_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
    {
        // Check if the operation was cancelled
        if (e.Cancelled)
        {
            Console.WriteLine("Upload was cancelled.");
            return;
        }

        // Check for errors
        if (e.Error != null)
        {
            Console.WriteLine("An error occurred: " + e.Error.Message);
            return;
        }

        // Get the result (response from the server)
        byte[] result = e.Result;
        string responseString = System.Text.Encoding.ASCII.GetString(result);
        Console.WriteLine("File uploaded successfully! Server response: " + responseString);
        Console.WriteLine("User token: " + e.UserState);
    }
}
                

See Also