HTTP uploading files (C#)

Yet another snippet from my evergrowing code library: How to programmatically upload (possibly multiple) files to an HTML form. I won't post the full code here, but you can download a zip file with a Visual Studio solution including a test app. The upload code is in a separate file (HttpFileUpload.cs), so it's easy to use even without VS.

The HttpFileUpload class provides three methods: UploadFile, UploadByteArray and Upload. The first two call the last one and are just shortcuts to the generic behavior of the upload code. Let's only take a look at the generic method here; the other two are trivial to figure out by reading the in-code documentation.

The Upload method takes four parameters: the target url, a cookie container, credential cache for passing logon information and the objects to be uploaded (as a params array of UploadSpec objects). UploadSpec objects can be constructed by passing either the pathname or the byte array. Note that even with the byte array form, you still have to specify a fictional filename for the receiving end (although leaving it empty isn't usually fatal). Also, you'll have to specify the name of the form field to which the uploaded data should be stuffed into.

An example call:

byte[] someByteData = GetSomeBytes();
new HttpFileUpload.UploadSpec(@"c:\windows\win.ini", "file1"),
new HttpFileUpload.UploadSpec(someByteData, "myfile.exe", "file2")

I acknowledge there are still some additional features that might be needed in the future. In case I end up enhancing the library, I'll post an update. Meanwhile, feel free to use the code for whatever needs you have; feedback is naturally welcome.

Edit 2005-03-20: The code has been updated to support form variable posting. See the new post on the subject.

October 17, 2004 В· Jouni Heikniemi В· 21 Comments
Posted in: .NET

21 Responses

  1. Anonymous - December 15, 2004

    doenst work….

  2. Alexandre - December 15, 2004

    Works very nice, thanks for your code !
    I just have to set the UserAgent propertie of the HttpWebRequest.

  3. Ed - January 6, 2005

    Thanks a bunch for this example. The fact that you have the CredentialCache included with a POST method, in your example makes it rise above many other examples.
    I was stuck until I came across this.
    Thanks again.

  4. Anonymous - May 3, 2005

    Can this snippet work for Https?

  5. Carlo - May 13, 2005

    thanks for your code.
    Is it possible to make it work via https?

  6. wolverine - June 3, 2005

    thank u for writin such a code
    it will b of use to many
    but hav u tried uploading quite large files with ur code. say around 90MB. it crashes.
    so could u modify ur code or suggest some modifications so that it can b used for uploading such large files
    thank u

  7. Jouni - June 3, 2005

    I need to test it on HTTPS some day.
    About 90 MB uploads: HTTP is ill-suited for that anyway. Don't do it.

  8. ellard - January 6, 2006

    how do i add fields like text to be sent also?

  9. ellard - January 6, 2006

    can you provide the server side script also?

  10. Sebastian.Moser - February 4, 2006

    as an relatively unexperienced c#-user, i'm happy to have found your script.
    if you're interested in what i'm doing with it:
    i work on an photo-upload-software for an event-photo-community, where official photographers take pictures from local events. this software will allow me to make the whole process of uploading pictures MUCH easier, as it resizes the pictures already on the photographer's computer without having to use an extra software.
    thanks a lot!!

  11. Grant - February 8, 2006

    Thanks a lot – i added the functionality to my program and it works well

  12. Jeff Binnig - July 19, 2006

    The new Upload.cs is very nice.
    The StringDictionary is spitting out lowercase "keys", so I had to convert to another object.

  13. Johan Deckmar - September 19, 2006

    Hi Jouni, great job on the HttpFileUpload! I love it ! :)

  14. Thuy - September 25, 2006

    Has anyone found a solution for https?

  15. Speng - January 30, 2007

    this is good!

  16. Helpme - February 3, 2007

    How do I setup the server side?
    so that files can be uploaded from my webpage

  17. Phil Kulak - February 9, 2007

    Dude, you rock! Thanks! Very intuitive class, and it works great.

  18. Good Conversation - July 3, 2007

    Dude, you rock! Thanks! Very intuitive class, and it works great.

  19. Alex Egg - October 4, 2007

    very clever to include the CookieCollection parameter!

  20. Michael - January 28, 2008

    Doesnt work. Tried to upload to my site using this program and i get an error

  21. Massimo Laboranti - February 6, 2008

    Thank you for your work, it's great.
    Hello from Italy!!!!!!!!

Leave a Reply