March 20, 2005

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.

Posted by Jouni Heikniemi at March 20, 2005 11:20 AM
.net
Comments

Would you please offer a web example for this code?

Posted by: Nick at June 30, 2005 04:44 PM

You mustn't use a StringDictionary, they convert the keys to lowercase.

Posted by: Jaykul at September 6, 2005 07:06 AM

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?

Posted by: opello at February 21, 2006 04:21 AM

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

Posted by: Junaid at June 5, 2006 08:26 AM

Your help was great to me. Thank You

Posted by: Misztal at September 21, 2006 01:26 PM

It's not really helping. How can do this? Examples! :D

Posted by: alu at November 1, 2006 09:20 PM

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);


Posted by: Franklin at December 22, 2006 12:53 AM

sdf

Posted by: at January 17, 2007 04:47 PM

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

Posted by: Ilya_M at March 10, 2007 12:57 AM

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

Posted by: Ilya_M at March 10, 2007 02:05 PM

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:
URL: http://localhost:8080/
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

Posted by: ian james at May 29, 2007 09:53 AM

Thanks for this sharing, i have problem when i tried to upload a large file (it works fine with small size). I think the problem is you try to write the post data to stream withou buffer:
postData.WriteTo(s);

How do i change this? How do i add buffer and write it to stream?

Posted by: Tony Ha at June 12, 2008 05:49 AM

Hi all,

I've just solved my issue, the problem is the http protocol version, here is my update code

//keep session
req.KeepAlive = false;
req.Timeout = Timeout.Infinite;
//use protocol version 10 instead of 11 by default
req.ProtocolVersion = HttpVersion.Version10;

Now i can upload file up to 1G. Hope this help for anyone who have same problem with me

Thnks,
Tony

Posted by: Tony Ha at June 13, 2008 09:55 AM
Post a comment









Remember personal info?