Click here to Skip to main content
Click here to Skip to main content

C# File Upload with form fields, cookies and headers

By , 18 Apr 2010
 

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;

/// <summary>
/// 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.
/// <code>Upload.PostFile(new Uri(http://mysite.com/myHandler), 
/// null, @"c:\temp\myPic.jgp", null, null, null, null);</code>
/// 
/// could be accomplished with this overload
/// 
/// <code>Upload.PostFile(new Uri(http://mysite.com/myHandler), 
/// @"c:\temp\myPic.jgp");</code>
/// 
/// I suggest after this brief introduction 
/// that you pull the full source from http://salient.codeplex.com.
/// </summary>
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

/// <summary>
/// This class contains methods excepted from Salient.Web.HttpLib.HttpRequestUtility
/// for demonstration purposes. Please see http://salient.codeplex.com for full 
/// implementation
/// </summary>
public static class Upload
{
    /// <summary>
    /// Uploads a stream using a multipart/form-data POST.
    /// </summary>
    /// <param name="requestUri"></param>
    /// <param name="postData">A NameValueCollection containing form fields 
    /// to post with file data</param>
    /// <param name="fileData">An open, positioned stream containing the file data</param>
    /// <param name="fileName">Optional, a name to assign to the file data.</param>
    /// <param name="fileContentType">Optional. 
    /// If omitted, registry is queried using <paramref name="fileName"/>. 
    /// If content type is not available from registry, 
    /// application/octet-stream will be submitted.</param>
    /// <param name="fileFieldName">Optional, 
    /// a form field name to assign to the uploaded file data. 
    /// If omitted the value 'file' will be submitted.</param>
    /// <param name="cookies">Optional, can pass null. Used to send and retrieve cookies. 
    /// Pass the same instance to subsequent calls to maintain state if required.</param>
    /// <param name="headers">Optional, headers to be added to request.</param>
    /// <returns></returns>
    /// 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();
        }
    }

    /// <summary>
    /// Uploads a file using a multipart/form-data POST.
    /// </summary>
    /// <param name="requestUri"></param>
    /// <param name="postData">A NameValueCollection containing 
    /// form fields to post with file data</param>
    /// <param name="fileName">The physical path of the file to upload</param>
    /// <param name="fileContentType">Optional. 
    /// If omitted, registry is queried using <paramref name="fileName"/>. 
    /// If content type is not available from registry, 
    /// application/octet-stream will be submitted.</param>
    /// <param name="fileFieldName">Optional, a form field name 
    /// to assign to the uploaded file data. 
    /// If omitted the value 'file' will be submitted.</param>
    /// <param name="cookies">Optional, can pass null. Used to send and retrieve cookies. 
    /// Pass the same instance to subsequent calls to maintain state if required.</param>
    /// <param name="headers">Optional, headers to be added to request.</param>
    /// <returns></returns>
    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);
        }
    }
    /// <summary>
    /// Attempts to query registry for content-type of supplied file name.
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    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)

About the Author

Sky Sanders
Software Developer (Senior) Salient Solutions
United States United States
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionI'm get errormemberTran Van Huy11 Jan '13 - 15:59 
GeneralProgressmemberLaserson26 Nov '10 - 0:22 
GeneralRe: ProgressmemberSky Sanders26 Nov '10 - 0:25 
GeneralGreat Work Except for the licensemembericetea9418 Apr '10 - 3:02 
GeneralRe: Great Work Except for the licensememberSky Sanders18 Apr '10 - 6:22 
I understand and I think you have a point.
 
Actually I did not think too much about this as I pulled it from a larger library that I am working on.
 
Licensing a snippet seems really silly now that you mention it, and this has been remedied.
 
Cheers.
GeneralGreat workmentorBrij16 Apr '10 - 7:26 
GeneralRe: Great workmemberSky Sanders17 Apr '10 - 4:56 

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

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