Uploading files doesn't work correctly

This is a discussion on Uploading files doesn't work correctly within the C# Programming forums, part of the General Programming Boards category; hi! since WebClient.UploadFile() always uploads the file into the form named "file", i can't use it. so i made my ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    244

    Uploading files doesn't work correctly

    hi!
    since WebClient.UploadFile() always uploads the file into the form named "file", i can't use it. so i made my own function:
    Code:
    	static private string UploadFile(string url, string path)
    		{
    			HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    
    			string boundary = DateTime.Now.Ticks.ToString("x");
    			byte[] boundary_ = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
    
    			string header = "Content-Disposition: form-data; name=\"archivo\"; filename=\"" + Path.GetFileName(path) + "\"\r\nContent-Type: application/octet-stream\r\n\r\n";
    			byte[] header_ = Encoding.ASCII.GetBytes(header);
    
    			req.ContentType = "multipart/form-data; boundary=" + boundary;
    			req.Method = "POST";
    			req.KeepAlive = true;
    			req.AllowAutoRedirect = false;
    			req.ContentLength = boundary_.Length * 2 + header_.Length + new FileInfo(path).Length;
    			Stream stream = req.GetRequestStream();
    
    			stream.Write(boundary_, 0, boundary_.Length);
    			stream.Write(header_, 0, header_.Length);
    
    			FileStream file = File.OpenRead(path);
    			byte[] buffer = new byte[4096];
    			int read = 0;
    			int prg = 0;
    			int count = 0;
    			DateTime last = DateTime.Now;
    
    			while ((read = file.Read(buffer, 0, buffer.Length)) > 0)
    			{
    				try
    				{
    					stream.Write(buffer, 0, read);
    				}
    				catch
    				{
    					SharedFunctions.Error("The server rejected the file at a status of " + SharedFunctions.SizeName(prg) + "/" + SharedFunctions.SizeName(Convert.ToInt32(file.Length)) + ".\nThis could be because the file exceeded the maximum upload file size of the server.");
    					stream = null;
    					break;
    				}
    				prg += read;
    				count += read;
    
    				int per = prg * 100 / Convert.ToInt32(file.Length);
    				prgUpload.Value = per;
    				lblUploadProgress.Text = per + " %";
    				if ((DateTime.Now - last).Seconds > 0)
    				{
    					last = DateTime.Now;
    					lblUploadSpeed.Text = SharedFunctions.SizeName(count) + "/s";
    					lblUploadTimeleft.Text = Convert.ToString(TimeSpan.FromSeconds(Convert.ToDouble((file.Length - prg) / count)));
    					count = 0;
    				}
    				Application.DoEvents();
    			}
    			file.Close();
    
    			if (stream != null)
    			{
    				stream.Write(boundary_, 0, boundary_.Length);
    				stream.Close();
    
    				WebResponse res = req.GetResponse();
    				string ret = res.Headers[HttpResponseHeader.Location];
    				res.Close();
    				return ret;
    			}
    			else
    			{
    				return "";
    			}
    		}
    you see the exception handler? thats where stream.Write() causes an exception "connection was aborted; connection was canceled".
    this usually happens to files above ~3 MB.

    does anyone know why this keeps happening?
    thank you so much!

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    It is most likely due to the http request size limit. This can be set inside your web.config file if I'm not entirely mistaken.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    it doesnt seem to crash at the same size. it's different. when i send 128 kb packages at once instead of 4 kb, it crashes later.

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    EDIT:

    Nevermind. This was ASP, not ASP.NET.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    this bug really makes my application worth nothing. every time i upload a file > ~3MB, it crashes.
    WebClient.UploadFile() doesn't work, because it allways uploads data to the form "file"...

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by Devils Child View Post
    this bug really makes my application worth nothing. every time i upload a file > ~3MB, it crashes.
    WebClient.UploadFile() doesn't work, because it allways uploads data to the form "file"...
    Save and rename?

    And I'm pretty sure this has something to do with the maximum request size. I'm almost positive. Let me grab a book I have on ASP.NET 2.0 and look it up.. sec.

    Yeah, here we go. Under <system.web> in your web.config, you should try putting the following:

    Code:
    <httpRuntime maxRequestLength="WhateverSizeYouNeedInKB" executionTimeout="MaxRequestDurationInSeconds"/>
    The first param is self-explanatory, and the latter simply sets the max time a request may be processed until it's shut down by the runtime. If you want to be uploading big files, you're going to want to consider the fact that some people have slow connections, in which case the executionTimeout value should be pretty high.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    i'm using window forms applications in C#, not asp.

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Do you "own" the server? That is, do you know how it's configured? The problem does not appear to be in your code; it appears that the server is terminating the connection for some reason that only it knows, and I would suggest you need to investigate on that end.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    i don't own the server, but if you upload the file using firefox, it works!
    so it must be my program. i've looked at it with wireshark and i'm the one who cancelles the connection.
    the exception "the connection was canceled: the connection was aborted" is thrown.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 01:46 PM
  2. 3 Files of OO Language
    By rayrayj52 in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2004, 02:08 PM
  3. How do you work with resource files???
    By Finchie_88 in forum Windows Programming
    Replies: 4
    Last Post: 10-23-2004, 03:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21