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(); HttpFileUpload.Upload( "http://localhost/myupload.cgi", null, null, 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
Anonymous - December 15, 2004
doenst work….
Alexandre - December 15, 2004
Works very nice, thanks for your code !
I just have to set the UserAgent propertie of the HttpWebRequest.
Ed - January 6, 2005
Dude,
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.
Anonymous - May 3, 2005
Can this snippet work for Https?
Carlo - May 13, 2005
Hi,
thanks for your code.
Is it possible to make it work via https?
wolverine - June 3, 2005
hi
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
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.
ellard - January 6, 2006
how do i add fields like text to be sent also?
ellard - January 6, 2006
can you provide the server side script also?
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!!
Grant - February 8, 2006
Thanks a lot – i added the functionality to my program and it works well
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.
Johan Deckmar - September 19, 2006
Hi Jouni, great job on the HttpFileUpload! I love it ! :)
Thuy - September 25, 2006
Has anyone found a solution for https?
Speng - January 30, 2007
this is good!
Helpme - February 3, 2007
How do I setup the server side?
so that files can be uploaded from my webpage
Phil Kulak - February 9, 2007
Dude, you rock! Thanks! Very intuitive class, and it works great.
Good Conversation - July 3, 2007
Dude, you rock! Thanks! Very intuitive class, and it works great.
Alex Egg - October 4, 2007
very clever to include the CookieCollection parameter!
Michael - January 28, 2008
Doesnt work. Tried to upload to my site using this program and i get an error
Massimo Laboranti - February 6, 2008
Thank you for your work, it's great.
Hello from Italy!!!!!!!!
Leave a Reply