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.
Posted by Jouni Heikniemi at October 17, 2004 10:15 PMdoenst work....
Posted by: at December 15, 2004 12:50 PMWorks very nice, thanks for your code !
I just have to set the UserAgent propertie of the HttpWebRequest.
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.
Posted by: Ed at January 6, 2005 08:42 PMCan this snippet work for Https?
Posted by: at May 3, 2005 10:00 AMHi,
thanks for your code.
Is it possible to make it work via https?
Posted by: Carlo at May 13, 2005 04:30 PMhi
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
Posted by: wolverine at June 3, 2005 07:44 PMI need to test it on HTTPS some day.
About 90 MB uploads: HTTP is ill-suited for that anyway. Don't do it.
Posted by: Jouni at June 3, 2005 10:48 PMDear sir, I want to upload my webpage file from my computer. Can you help me?
Posted by: imtiwapang at November 1, 2005 08:11 AMdear sir I want to upload some web pages
Posted by: imtiwapang at November 1, 2005 08:13 AMhow do i add fields like text to be sent also?
Posted by: ellard at January 6, 2006 04:38 AMcan you provide the server side script also?
Posted by: ellard at January 6, 2006 04:47 AMas 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!!
Posted by: Sebastian.Moser at February 4, 2006 06:13 PMThanks a lot - i added the functionality to my program and it works well
Posted by: Grant at February 8, 2006 04:23 AMair purifier http://www.u-blog.net/airpurifier
bare escentuals http://www.u-blog.net/bareescentuals
bare minerals http://www.u-blog.net/bareminerals
bathroom vanities http://www.u-blog.net/bathroomvanities
colloidal silver http://www.u-blog.net/colloidalsilver
colon cleanse http://www.u-blog.net/coloncleanse
computer armoire http://www.u-blog.net/computerarmoire
powerball lottery http://www.u-blog.net/powerballlottery
beads http://www.u-blog.net/beads
bodybuilders http://www.u-blog.net/bodybuilders
cheep flights http://www.u-blog.net/cheepflights
cheep tickets http://www.u-blog.net/cheeptickets
Heh, I found the WebRobot component, at http://foxtrot-xray.com/web-robot before I saw this page.
Still, I am very happy with the WebRobot. And it's pretty useful.
The new Upload.cs is very nice.
The StringDictionary is spitting out lowercase "keys", so I had to convert to another object.
Posted by: Jeff Binnig at July 19, 2006 09:44 PMHi Jouni, great job on the HttpFileUpload! I love it ! :)
Posted by: Johan Deckmar at September 19, 2006 04:52 PMHas anyone found a solution for https?
Posted by: Thuy at September 25, 2006 09:24 PMthis is good!
Posted by: Speng at January 30, 2007 05:43 PMHow do I setup the server side?
so that files can be uploaded from my webpage
Posted by: Helpme at February 3, 2007 10:43 AMDude, you rock! Thanks! Very intuitive class, and it works great.
Posted by: Phil Kulak at February 9, 2007 02:28 AMDude, you rock! Thanks! Very intuitive class, and it works great.
Posted by: Good Conversation at July 3, 2007 02:14 AMvery clever to include the CookieCollection parameter!
Posted by: Alex Egg at October 4, 2007 12:21 AM7LOx0X cshoqmnavddp, [url=http://ctfwwwbpagbd.com/]ctfwwwbpagbd[/url], [link=http://ylpifnsqkety.com/]ylpifnsqkety[/link], http://bdmhaxnhgyca.com/
Posted by: fqcqnxbvt at January 14, 2008 05:12 AM7LOx0X cshoqmnavddp, [url=http://ctfwwwbpagbd.com/]ctfwwwbpagbd[/url], [link=http://ylpifnsqkety.com/]ylpifnsqkety[/link], http://bdmhaxnhgyca.com/
Posted by: fqcqnxbvt at January 14, 2008 05:13 AMDoesnt work. Tried to upload to my site using this program and i get an error
Posted by: Michael at January 28, 2008 02:35 AMfree porn movie[url=http://nikaxxx.fbhosting.com]porn movie free[/url]porn star video[url=http://kiska.blogvis.com]porn star movie[/url]anal sex video[url=http://nikita.outblogger.com]sex video anal[/url]anal sex[url=http://www.yourblogs.net/anal]video anal[/url]lesbian sex[url=http://www.yourblogs.net/solly]lesbian video[/url]lesbian sex[url=http://www.yourblogs.net/lolita]lesbian video[/url]porn star[url=http://porn.blogsarena.com]porn star movie top[/url]
Posted by: derfgw at February 4, 2008 12:25 AMThank you for your work, it's great.
Hello from Italy!!!!!!!!
Great work. Thanks for sharing it with us.
Posted by: teen sex at February 28, 2008 03:57 PMWhat can I say?....Congratulation you did a great job.
Posted by: london escort at February 29, 2008 04:16 PMNice job. Thanks for sharing it. You did a good thing for all of us.
Posted by: strip clubs at March 3, 2008 03:12 PM[url=http://gal3.mnogoporn.net/anal-matrimonio-sexo.php]http://gal3.mnogoporn.net/anal-matrimonio-sexo.php[/url] http://gal3.mnogoporn.net/anal-matrimonio-sexo.php [url=http://gal1.mnogoporn.net/05-10-2008-364.php]http://gal1.mnogoporn.net/05-10-2008-364.php[/url] http://gal1.mnogoporn.net/05-10-2008-364.php [url=http://gal2.mnogoporn.net/gay_interracial_sucking.php]http://gal2.mnogoporn.net/gay_interracial_sucking.php[/url] http://gal2.mnogoporn.net/gay_interracial_sucking.php [url=http://gal5.mnogoporn.net/guide_sex_tifton.php]http://gal5.mnogoporn.net/guide_sex_tifton.php[/url] http://gal5.mnogoporn.net/guide_sex_tifton.php [url=http://gal2.mnogoporn.net/gay_marriages_legalize.php]http://gal2.mnogoporn.net/gay_marriages_legalize.php[/url] http://gal2.mnogoporn.net/gay_marriages_legalize.php [url=http://gal3.mnogoporn.net/iran-sex-gallery.php]http://gal3.mnogoporn.net/iran-sex-gallery.php[/url] http://gal3.mnogoporn.net/iran-sex-gallery.php [url=http://gal4.mnogoporn.net/boarding-school-teen.php]http://gal4.mnogoporn.net/boarding-school-teen.php[/url] http://gal4.mnogoporn.net/boarding-school-teen.php [url=http://gal2.mnogoporn.net/fourteen_carat_mind.php]http://gal2.mnogoporn.net/fourteen_carat_mind.php[/url] http://gal2.mnogoporn.net/fourteen_carat_mind.php [url=http://gal1.mnogoporn.net/02-18-2008-672.php]http://gal1.mnogoporn.net/02-18-2008-672.php[/url] http://gal1.mnogoporn.net/02-18-2008-672.php [url=http://gal1.mnogoporn.net/03-17-2008-997.php]http://gal1.mnogoporn.net/03-17-2008-997.php[/url] http://gal1.mnogoporn.net/03-17-2008-997.php [url=http://gal1.mnogoporn.net/09-08-2007-335.php]http://gal1.mnogoporn.net/09-08-2007-335.php[/url] http://gal1.mnogoporn.net/09-08-2007-335.php [url=http://gal1.mnogoporn.net/07-11-2007-219.php]http://gal1.mnogoporn.net/07-11-2007-219.php[/url] http://gal1.mnogoporn.net/07-11-2007-219.php [url=http://gal3.mnogoporn.net/new-sex-story.php]http://gal3.mnogoporn.net/new-sex-story.php[/url] http://gal3.mnogoporn.net/new-sex-story.php [url=http://gal4.mnogoporn.net/young-teen-photo.php]http://gal4.mnogoporn.net/young-teen-photo.php[/url] http://gal4.mnogoporn.net/young-teen-photo.php [url=http://mnogoporn.net/8825386.php]http://mnogoporn.net/8825386.php[/url] http://mnogoporn.net/8825386.php
Posted by: Preveder at May 19, 2008 10:52 PM[url=http://gal2.pornpreved.com/better_sex_guide.php]http://gal2.pornpreved.com/better_sex_guide.php[/url] http://gal2.pornpreved.com/better_sex_guide.php [url=http://pornpreved.com/free-sex-tetris.php]http://pornpreved.com/free-sex-tetris.php[/url] http://pornpreved.com/free-sex-tetris.php [url=http://gal3.pornpreved.com/Apr-30-08-55.php]http://gal3.pornpreved.com/Apr-30-08-55.php[/url] http://gal3.pornpreved.com/Apr-30-08-55.php [url=http://gal2.pornpreved.com/sexy_black_haired.php]http://gal2.pornpreved.com/sexy_black_haired.php[/url] http://gal2.pornpreved.com/sexy_black_haired.php [url=http://gal2.pornpreved.com/adult_sex_cams.php]http://gal2.pornpreved.com/adult_sex_cams.php[/url] http://gal2.pornpreved.com/adult_sex_cams.php [url=http://gal1.pornpreved.com/push-up-sexy.php]http://gal1.pornpreved.com/push-up-sexy.php[/url] http://gal1.pornpreved.com/push-up-sexy.php [url=http://pornpreved.com/miss-teen-alabama.php]http://pornpreved.com/miss-teen-alabama.php[/url] http://pornpreved.com/miss-teen-alabama.php [url=http://gal2.pornpreved.com/sex_scenes_dvd.php]http://gal2.pornpreved.com/sex_scenes_dvd.php[/url] http://gal2.pornpreved.com/sex_scenes_dvd.php [url=http://gal1.pornpreved.com/female-teen-monologue.php]http://gal1.pornpreved.com/female-teen-monologue.php[/url] http://gal1.pornpreved.com/female-teen-monologue.php [url=http://pornpreved.com/naked-male-sex.php]http://pornpreved.com/naked-male-sex.php[/url] http://pornpreved.com/naked-male-sex.php [url=http://gal2.pornpreved.com/sexy_party_gallery.php]http://gal2.pornpreved.com/sexy_party_gallery.php[/url] http://gal2.pornpreved.com/sexy_party_gallery.php [url=http://pornpreved.com/buddy-butt-gay.php]http://pornpreved.com/buddy-butt-gay.php[/url] http://pornpreved.com/buddy-butt-gay.php [url=http://gal1.pornpreved.com/robin-williams-gay.php]http://gal1.pornpreved.com/robin-williams-gay.php[/url] http://gal1.pornpreved.com/robin-williams-gay.php [url=http://gal2.pornpreved.com/orgasm_teen_toying.php]http://gal2.pornpreved.com/orgasm_teen_toying.php[/url] http://gal2.pornpreved.com/orgasm_teen_toying.php [url=http://gal5.pornpreved.com/paginas_de_sexo.php]http://gal5.pornpreved.com/paginas_de_sexo.php[/url] http://gal5.pornpreved.com/paginas_de_sexo.php
Posted by: preveder at May 21, 2008 06:06 PM