Http file upload with parameters
An update to my old code sample for HTTP uploads: you can now post form variables with the files. The new version of Upload.cs contains a method overload that allows you to pass in a StringDictionary of POST parameters. Some basic instructions for use are available at the old post. This file will replace the current Upload.cs in JHLib – until the next release, you can also take a look at the current version of the library for some usage examples of the upload code in general.
March 20, 2005
В·
Jouni Heikniemi В·
9 Comments
Posted in: .NET


9 Responses
Nick - June 30, 2005
Would you please offer a web example for this code?
Jaykul - September 6, 2005
You mustn't use a StringDictionary, they convert the keys to lowercase.
opello - February 21, 2006
Great code, grabbing your new version with variables.
One thing that I was curious about was your boundary creation. Why you didn't just use:
Guid.NewGuid().ToString("N")
but replaced the hyphens with String.Replace, which I would think slower than using the Guid's own formatting routine?
Junaid - June 5, 2006
Hi Jouni,
I want to implement the progerss bar during file upload in the new version of Upload.cs that contains a method overload that allows you to pass in a StringDictionary of POST parameters.
I implemented in one small application, its working fine…. I want to know how we can implement the progress bar….
Your help will be great to me.
anyway…. Great coding by you..
Thanks & Regards,
Junaid
alu - November 1, 2006
It's not really helping. How can do this? Examples! :D
Franklin - December 22, 2006
Great Code!!!
But I'm having a problem when I try to use GetResponse Method, it always send me Internal Server Error HTTP 500.
I have add UserAgent and timeout but still I'm receive the same error message.
I'm trying to upload a file, I send all the require information, Viewstate, cookies, filename everything.
Can anyone help me with this.
webRequest.ContentLength = postData.Length;
using (Stream s = webRequest.GetRequestStream())
postData.WriteTo(s);
postData.Close();
webRequest.Timeout = 10000;
webRequest.UserAgent = "Mozilla/4.0+";
return (req.GetResponse() as HttpWebResponse);
Ilya_M - March 10, 2007
Hi
First of all – thank you for the code you supplied.
Second – I have a problem and I was hopping you could help me to solve it: I'm using this code to upload xml file from PDA to a Apache Webserver that runs on my local machine. The function successfully finished but no file appears on the web server.
Can you advice ?
Thanks
Ilya_M - March 10, 2007
One more question – I managed to make this work in this way – I have apache server and while it receives PUT http request, it triggers perl script that should take care of upload.I entered in this script logging and I do see the content of the data I'm sending but … it sends 310 bytes while client can read only 302, i.e. 8 byte less than exists. Does somebody have any idea why ? This make the read loop to be infinite.
thanks in advance
ian james - May 29, 2007
Hi,
I am new to .NET.
I have tried your code. But I am getting an error at line "return (req.GetResponse() as HttpWebResponse);" in the Upload() method.
The error is:
"An unhandled excption of type 'System.Net.WebException' occured in system.dll"
"Additional Information: The remote server returned an error: (405)Method Not Allowed."
I am using the old sample code (without updation). I can't understand what should I enter in the 'Fieldname' field. The following are my input details:
File: C:\Documents and Settings\User\Desktop\header.js
Fieldname: header
My ultimate aim is to upload a file to the server using http post method.
Could you help me please?
Thanks