ASP.NET Upload Frequently Asked Questions

Complete FAQ for file uploading with ASP.NET 1.x-3.5

Upload files using an RFC 1867 POST request with HttpWebRequest/WebClient

Share this page with friends, or bookmark it for yourself:

You don't have to write any code to format or upload browser based uploads – the browser takes care of that. But what if you want to simulate a browser upload request, or manually upload using code? To do this, you need to create and submit a request using the RFC 1867 multipart/mime-encoded formatting standard.

There are two ways to upload files in .NET:

  • The easy simple way, using .NET's WebClient.UploadFile method. This works for single files with no other form variables. It also requires a local file to exist to be able to upload.
  • The advanced powerful way. If you want to upload multiple files in one request, pass other form variables up with the request, or upload from memory, .NET's built in upload method won't suffice. You'll need to format the request yourself and upload using the HttpWebRequest class. See below for an example of this.

Simple uploading with WebClient.UploadFile

Call the method, passing in the URL and the name of the file to upload:

WebClient client = new WebClient();

byte[] responseBinary = client.UploadFile(url, fileName);

string response = Encoding.UTF8.GetString(responseBinary);

Advanced uploading with HttpWebRequest

The UploadHelper library is part of the source code download available on this site. This library provides an Upload method that can upload multiple files and form variables. It uses streams and other techniques so files are never loaded into memory, dramatically reducing memory usage and increasing scalability and performance. Grab the source archive, build it, and add a reference to the UploadHelper assembly to your project.

To use the UploadHelper library, set up your request parameters and call the UploadHelper.Upload method:

UploadFile[] files = new UploadFile[]
{
    new UploadFile(fileName1),
    new UploadFile(fileName2)
};

NameValueCollection form = new NameValueCollection();

form["name1"] = "value1";
form["name2"] = "xyzzy";

string response = UploadHelper.Upload(url, files, form);

If you want more control over the request, to set credentials or cookies for example, you can create the request object manually, set it up, and then pass it to the other Upload method overload to do the actual uploading:

UploadFile[] files = new UploadFile[]
{
    new UploadFile(fileName1),
    new UploadFile(fileName2)
};

NameValueCollection form = new NameValueCollection();

form["name1"] = "value1";
form["name2"] = "xyzzy";

HttpWebRequest req = WebRequest.Create(url);

// set credentials/cookies etc.

HttpWebResponse resp = UploadHelper.Upload(req, files, form);

using (Stream s = resp.GetResponseStream())
using (StreamReader sr = new StreamReader(s))
{
    string response = sr.ReadToEnd();
}
SlickUpload ASP.NET Upload Component

SlickUpload

Painless ASP.NET file uploads

  • Handle large files
  • Upload with progress bar
  • Gracefully handle cancellation and errors
  • Stream directly to file or database
  • Drag-drop simple, yet highly customizable
  • Solid, responsive support

» more info | live demo | download