Uploading files to a web form via GET/POST with C#
Okay, so I had a few problems today getting a simple C# application of mine to do what I wanted it to do. To make a long story short, I had some issues getting a WebRequest object to upload a slew of files via POST to a web-based form. Turns out that there is no simple way to do this in .Net – which is quite surprising because it’s quite easy to do just about everything in .Net. So, I did what every good developer does…I rolled my own class to take care of the problem: the JonnyFunFun.Net.FormPost class.
The class makes it easy for you to POST/GET variables to a web-based form with or without uploading files. Take for example this code:
List<string> FilesToUpload = new List<string>();
FilesToUpload.Add("c:\\debug.txt");
FilesToUpload.Add("c:\\upload_me.jpg");
FormPost form = new FormPost("http://www.jonnyfunfun.com/");
form.Method = FormPost.FormMethods.POST;
Dictionary<string, string> FormVariables = new Dictionary<string, string>();
FormVariables.Add("var1", "one");
FormVariables.Add("var2", "two");
WebResponse resp = form.PostWithFiles(FormVariables, FilesToUpload);
This code sends the var1 and var2 values in the FormVariables dictionary along with all the files specified in the FilesToUpload list. To make everybody else’s life easier, I’ve posted the class code and put it up using the GNU GPL. The only thing I ask for is that if you decide to use the code, or a derivative of it, in your own software project that you simply send me an e-mail and let me know! I like to see where my code ends up, and it makes a great thing to add to my resume! So, what are you waiting for – get the code!
C Application,
Class Code,
Debug,
Derivitive,
Dictionary,
Dictionary Files,
E Mail,
Gnu Gpl,
Jpg,
List Txt,
Lt,
New List,
Own Software,
Resume,
Send Mail,
Simple C,
Sleu,
Slew,
Software Project,
String String,
Upload,
Upload Files,
Uploading Files,
Variables,
Web Based