Click here to Skip to main content
Click here to Skip to main content
Technical Blog

C# File Upload with form fields, cookies and headers

, 18 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Full featured HTTP upload utility

In it's most basic form, uploading a file or other data to an HTTP form handler using managed code is quite simple using the System.Net.WebClient class.

Listing 1: SimpleWebClientUpload

public void SimpleWebClientUpload()
{
    string filePath = Path.GetFullPath("TestFiles/TextFile1.txt");
    var client = new WebClient();   
    client.UploadFile("http://localhost/myhandler.ashx", filePath);
}

It is a common case that some cookies need to be maintained between requests or perhaps a header needs to be added to the request. This is possible using the WebClient class but the technique is not immediately obvious.

The trick is to create a class that inherits WebClient and overrides WebClient.GetWebRequest.

You can then add your cookies or headers to the request before it is sent.

Listing 2: CustomWebClient

// usage example 

string filePath = Path.GetFullPath("TestFiles/TextFile1.txt");

// possibly an existing CookieContainer containing authentication
// or session cookies
var cookies = new CookieContainer();

var client = new CustomWebClient(cookies);

// existing cookies are sent
client.UploadFile(_postHandlerUri, filePath);

// cookies is an instance of a reference type so it now
// contains updated/new cookies resulting from the upload   

public class CustomWebClient : WebClient
{
    private CookieContainer _cookies;

    public CustomWebClient(CookieContainer cookies)
    {
        _cookies = cookies;
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
        request.CookieContainer = _cookies;
        return request;
    }
}

If this fulfills your requirements, then you are good to go.

Unfortunately, most file upload scenarios are HTML form based and may contain form fields in addition to the file data. This is where WebClient falls flat. After review of the source code for WebClient, it is obvious that there is no possibility of reusing it to perform a file upload including additional form fields.

So, the only option is to create a custom implementation that conforms to rfc1867, rfc2388 and the W3C multipart/form-data specification that will enable file upload with additional form fields and exposes control of cookies and headers.

Enumeration of the details of the implementation are beyond the intended scope of this posting, but the references are linked above and the code is quite brief and clear so to those interested, the process should be clear.

Note: This class is an excerpt from a slightly larger library, Salient.Web.HttpLib.HttpRequestUtility, that contains, amongst other functionality, many convenience overloads that eliminate unused parameters you can see in the usage implementation.

Listing 3: Upload example usage

using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;

/// <span class="code-SummaryComment"><summary>
</span>/// Presents basic usage of PostFile().  
/// 
/// Note: the accompanying class Upload is an excerpt of a larger class, 
/// Salient.Web.HttpLib.HttpRequestUtility, 
/// that contains many convenience overloads which have been omitted for brevity.
/// 
/// e.g.
/// <span class="code-SummaryComment"><code>Upload.PostFile(new Uri(http://mysite.com/myHandler), 
</span>/// null, @"c:\temp\myPic.jgp", null, null, null, null);<span class="code-SummaryComment"></code>
</span>/// 
/// could be accomplished with this overload
/// 
/// <span class="code-SummaryComment"><code>Upload.PostFile(new Uri(http://mysite.com/myHandler), 
</span>/// @"c:\temp\myPic.jgp");<span class="code-SummaryComment"></code>
</span>/// 
/// I suggest after this brief introduction 
/// that you pull the full source from http://salient.codeplex.com.
/// <span class="code-SummaryComment"></summary>
</span>public class UploadUsage
{
    public void UploadFile()
    {
        string filePath = Path.GetFullPath("TestFiles/TextFile1.txt");
        string responseText;
        Upload.PostFile(new Uri(http://localhost/myhandler.ashx), 
		null, filePath, null, null, null, null);
    }

    public void UploadFileWithFormFields()
    {
        string filePath = Path.GetFullPath("TestFiles/TextFile1.txt");

        // this represents fields from a form
        NameValueCollection postData = new NameValueCollection();
        postData.Add("fieldName", "fieldValue");

        string responseText;
        Upload.PostFile(new Uri(http://localhost/myhandler.ashx), 
		postData, filePath, null, null, null, null);
    }

    public void UploadFileWithFormFieldsCookiesAndHeaders()
    {
        string filePath = Path.GetFullPath("TestFiles/TextFile1.txt");

        // this represents fields from a form
        NameValueCollection postData = new NameValueCollection();
        postData.Add("fieldName", "fieldValue");

        // this could be an existing CookieContainer used in a previous request
        // to contain session or other cookies. Typically used to maintain
        // session state across several requests.
        CookieContainer cookies = new CookieContainer();

        // you can send additional request headers with the headers collection
        NameValueCollection headers = new NameValueCollection();
        headers.Add("x-headerName", "header value");

        // content type of the posted file.
        // if null, the content type will be determined by the filename.
        // defaults to application/octet-stream
        const string fileContentType = null;

        // the key used to identify this file. typically unused.
        // if null, 'file' will be submitted.
        const string fileFieldName = null;

        string responseText;
        Upload.PostFile(new Uri(http://localhost/myhandler.ashx), 
	postData, filePath, fileContentType, fileFieldName,
                        cookies, headers);
    }

    public void UploadStream()
    {
        // You may also upload data from an open and positioned stream
        MemoryStream fileData = new MemoryStream(new byte[] {0, 1, 2, 3, 4});

        // The name to associate with the uploaded data. 
        // Content-type will be determined from this value
        const string fileName = "foo.bin";


        Upload.PostFile(new Uri(http://localhost/myhandler.ashx), 
	null, fileData, fileName, null, null, null, null);
    }

    public void UploadAndGetResponse()
    {
        MemoryStream fileData = new MemoryStream(new byte[] {0, 1, 2, 3, 4});
        const string fileName = "foo.bin";

        using (
            WebResponse response = Upload.PostFile
	     (new Uri("http://localhost/myhandler.ashx"), null, fileData, fileName,
               null, null, null, null))
        {
            // the stream returned by WebResponse.GetResponseStream 
            // will contain any content returned by the server after upload

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string responseText = reader.ReadToEnd();
            }
        }
    }
}

Listing 4: Upload.cs

// /*!
//  * Project: Salient.Web.HttpLib
//  * http://salient.codeplex.com
//  *
//  * Date: April 11 2010 
//  */

#region

using System;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.Win32;

#endregion

/// <span class="code-SummaryComment"><summary>
</span>/// This class contains methods excepted from Salient.Web.HttpLib.HttpRequestUtility
/// for demonstration purposes. Please see http://salient.codeplex.com for full 
/// implementation
/// <span class="code-SummaryComment"></summary>
</span>public static class Upload
{
    /// <span class="code-SummaryComment"><summary>
</span>    /// Uploads a stream using a multipart/form-data POST.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><param name="requestUri"></param>
</span>    /// <span class="code-SummaryComment"><param name="postData">A NameValueCollection containing form fields 
</span>    /// to post with file data<span class="code-SummaryComment"></param>
</span>    /// <span class="code-SummaryComment"><param name="fileData">An open, positioned stream containing the file data</param>
</span>    /// <span class="code-SummaryComment"><param name="fileName">Optional, a name to assign to the file data.</param>
</span>    /// <span class="code-SummaryComment"><param name="fileContentType">Optional. 
</span>    /// If omitted, registry is queried using <span class="code-SummaryComment"><paramref name="fileName"/>. 
</span>    /// If content type is not available from registry, 
    /// application/octet-stream will be submitted.<span class="code-SummaryComment"></param>
</span>    /// <span class="code-SummaryComment"><param name="fileFieldName">Optional, 
</span>    /// a form field name to assign to the uploaded file data. 
    /// If omitted the value 'file' will be submitted.<span class="code-SummaryComment"></param>
</span>    /// <span class="code-SummaryComment"><param name="cookies">Optional, can pass null. Used to send and retrieve cookies. 
</span>    /// Pass the same instance to subsequent calls to maintain state if required.<span class="code-SummaryComment"></param>
</span>    /// <span class="code-SummaryComment"><param name="headers">Optional, headers to be added to request.</param>
</span>    /// <span class="code-SummaryComment"><returns></returns>
</span>    /// Reference: 
    /// http://tools.ietf.org/html/rfc1867
    /// http://tools.ietf.org/html/rfc2388
    /// http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
    /// 
    public static WebResponse PostFile
	(Uri requestUri, NameValueCollection postData, Stream fileData, string fileName,
         	string fileContentType, string fileFieldName, CookieContainer cookies, 
	NameValueCollection headers)
    {
        HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(requestUri);

        string ctype;

        fileContentType = string.IsNullOrEmpty(fileContentType)
                              ? TryGetContentType(fileName, out ctype) ? 
				ctype : "application/octet-stream"
                              : fileContentType;

        fileFieldName = string.IsNullOrEmpty(fileFieldName) ? "file" : fileFieldName;

        if (headers != null)
        {
            // set the headers
            foreach (string key in headers.AllKeys)
            {
                string[] values = headers.GetValues(key);
                if (values != null)
                    foreach (string value in values)
                    {
                        webrequest.Headers.Add(key, value);
                    }
            }
        }
        webrequest.Method = "POST";

        if (cookies != null)
        {
            webrequest.CookieContainer = cookies;
        }

        string boundary = "----------" + DateTime.Now.Ticks.ToString
					("x", CultureInfo.InvariantCulture);

        webrequest.ContentType = "multipart/form-data; boundary=" + boundary;

        StringBuilder sbHeader = new StringBuilder();

        // add form fields, if any
        if (postData != null)
        {
            foreach (string key in postData.AllKeys)
            {
                string[] values = postData.GetValues(key);
                if (values != null)
                    foreach (string value in values)
                    {
                        sbHeader.AppendFormat("--{0}\r\n", boundary);
                        sbHeader.AppendFormat("Content-Disposition: 
			form-data; name=\"{0}\";\r\n\r\n{1}\r\n", key,
                                              value);
                    }
            }
        }

        if (fileData != null)
        {
            sbHeader.AppendFormat("--{0}\r\n", boundary);
            sbHeader.AppendFormat("Content-Disposition: form-data; 
			name=\"{0}\"; {1}\r\n", fileFieldName,
                                  string.IsNullOrEmpty(fileName)
                                      ?
                                          ""
                                      : string.Format(CultureInfo.InvariantCulture, 
					"filename=\"{0}\";",
                                                      Path.GetFileName(fileName)));

            sbHeader.AppendFormat("Content-Type: {0}\r\n\r\n", fileContentType);
        }

        byte[] header = Encoding.UTF8.GetBytes(sbHeader.ToString());
        byte[] footer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
        long contentLength = header.Length + (fileData != null ? 
			fileData.Length : 0) + footer.Length;

        webrequest.ContentLength = contentLength;

        using (Stream requestStream = webrequest.GetRequestStream())
        {
            requestStream.Write(header, 0, header.Length);


            if (fileData != null)
            {
                // write the file data, if any
                byte[] buffer = new Byte[checked((uint)Math.Min(4096, 
					(int)fileData.Length))];
                int bytesRead;
                while ((bytesRead = fileData.Read(buffer, 0, buffer.Length)) != 0)
                {
                    requestStream.Write(buffer, 0, bytesRead);
                }
            }

            // write footer
            requestStream.Write(footer, 0, footer.Length);

            return webrequest.GetResponse();
        }
    }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Uploads a file using a multipart/form-data POST.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><param name="requestUri"></param>
</span>    /// <span class="code-SummaryComment"><param name="postData">A NameValueCollection containing 
</span>    /// form fields to post with file data<span class="code-SummaryComment"></param>
</span>    /// <span class="code-SummaryComment"><param name="fileName">The physical path of the file to upload</param>
</span>    /// <span class="code-SummaryComment"><param name="fileContentType">Optional. 
</span>    /// If omitted, registry is queried using <span class="code-SummaryComment"><paramref name="fileName"/>. 
</span>    /// If content type is not available from registry, 
    /// application/octet-stream will be submitted.<span class="code-SummaryComment"></param>
</span>    /// <span class="code-SummaryComment"><param name="fileFieldName">Optional, a form field name 
</span>    /// to assign to the uploaded file data. 
    /// If omitted the value 'file' will be submitted.<span class="code-SummaryComment"></param>
</span>    /// <span class="code-SummaryComment"><param name="cookies">Optional, can pass null. Used to send and retrieve cookies. 
</span>    /// Pass the same instance to subsequent calls to maintain state if required.<span class="code-SummaryComment"></param>
</span>    /// <span class="code-SummaryComment"><param name="headers">Optional, headers to be added to request.</param>
</span>    /// <span class="code-SummaryComment"><returns></returns>
</span>    public static WebResponse PostFile
	(Uri requestUri, NameValueCollection postData, string fileName,
         string fileContentType, string fileFieldName, CookieContainer cookies,
         NameValueCollection headers)
    {
        using (FileStream fileData = File.Open
		(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            return PostFile(requestUri, postData, fileData, 
		fileName, fileContentType, fileFieldName, cookies,
                            headers);
        }
    }
    /// <span class="code-SummaryComment"><summary>
</span>    /// Attempts to query registry for content-type of supplied file name.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><param name="fileName"></param>
</span>    /// <span class="code-SummaryComment"><param name="contentType"></param>
</span>    /// <span class="code-SummaryComment"><returns></returns>
</span>    public static bool TryGetContentType(string fileName, out string contentType)
    {
        try
        {
            RegistryKey key = Registry.ClassesRoot.OpenSubKey
				(@"MIME\Database\Content Type");

            if (key != null)
            {
                foreach (string keyName in key.GetSubKeyNames())
                {
                    RegistryKey subKey = key.OpenSubKey(keyName);
                    if (subKey != null)
                    {
                        string subKeyValue = (string)subKey.GetValue("Extension");

                        if (!string.IsNullOrEmpty(subKeyValue))
                        {
                            if (string.Compare(Path.GetExtension
				(fileName).ToUpperInvariant(),
                                     subKeyValue.ToUpperInvariant(), 
				StringComparison.OrdinalIgnoreCase) ==
                                0)
                            {
                                contentType = keyName;
                                return true;
                            }
                        }
                    }
                }
            }
        }
        // ReSharper disable EmptyGeneralCatchClause
        catch
        {
            // fail silently
            // TODO: rethrow registry access denied errors
        }
        // ReSharper restore EmptyGeneralCatchClause
        contentType = "";
        return false;
    }
}

Using this class, you can easily post file data and form fields to form handlers and have full control of the cookies and headers that are sent/received.

Full implementation and tests can be found at http://salient.codeplex.com.

An alternate implementation of this code with a working Visual Studio 2008 solution can be found @ http://www.codeproject.com/KB/aspnet/Salient-Web-HttpLib-Intro.aspx.

History 

  • 04-18-2010 Removed licensing restriction 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sky Sanders
Software Developer (Senior) Salient Solutions
United States United States
My name is Sky Sanders and I am an end-to-end, front-to-back software solutions architect with more than 20 years experience in IT infrastructure and software development, the last 10 years being focused primarily on the Microsoft .NET platform.
 
My motto is 'I solve problems.' and I am currently available for hire.
 
I can be contacted at sky.sanders@gmail.com

Comments and Discussions

 
QuestionI'm get error PinmemberTran Van Huy11-Jan-13 16:59 
GeneralProgress PinmemberLaserson26-Nov-10 1:22 
GeneralRe: Progress PinmemberSky Sanders26-Nov-10 1:25 
Not sure I understand your question. Can you elaborate?
GeneralGreat Work Except for the license Pinmembericetea9418-Apr-10 4:02 
GeneralRe: Great Work Except for the license PinmemberSky Sanders18-Apr-10 7:22 
GeneralGreat work PinmentorBrij16-Apr-10 8:26 
GeneralRe: Great work PinmemberSky Sanders17-Apr-10 5:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 18 Apr 2010
Article Copyright 2010 by Sky Sanders
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid