[ACCEPTED]-Big files uploading (WebException: The connection was closed unexpectedly)-httpwebrequest

Accepted answer
Score: 12

Update : nope, there is no redirect.

screenshot

Read 13 RFC2388 few times, rewrote the code and it finally 12 worked (i guess the trouble was in utf-read 11 trailing boundary instead of correct 7 bit 10 ascii). Hooray? Nope :(. Only small files 9 are transfered, big ones throwing "The 8 connection was closed unexpectedly".

System.Net.WebException was unhandled by user code
  Message="The underlying connection was closed: The connection was closed unexpectedly."
  Source="Uploader"
  StackTrace:
   at Uploader.Upload.ProcessUpload(String FilePath, String description, String password) in F:\MyDocuments\Visual Studio 2008\Projects\Uploader\Uploader.cs:line 96
   at Uploader.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in F:\MyDocuments\Visual Studio 2008\Projects\Uploader\Form1.cs:line 45
   at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument) 

I 7 know that's a bug with .net stack and few 6 solutions exists :

1) increase both Timeout 5 and ReadWriteTimeout of request

2) assign 4 request.KeepAlive = false and System.Net.ServicePointManager.Expect100Continue 3 = false

3) set ProtocolVersion to 1.0 But 2 neither one of them nor all of them altogether 1 help in my case. Any ideas?

EDIT - Source code:

// .. request created, required params applied
httpWebRequest.ProtocolVersion = HttpVersion.Version10; // fix 1
httpWebRequest.KeepAlive = false; // fix 2
httpWebRequest.Timeout = 1000000000; // fix 3
httpWebRequest.ReadWriteTimeout = 1000000000; // fix 4
// .. request processed, data written to request stream
string strResponse = "";            
try
{
    using (WebResponse httpResponse = httpWebRequest.GetResponse()) // error here
        {
            using (Stream responseStream = httpResponse.GetResponseStream())
            {
                using (StreamReader streamReader = new StreamReader(responseStream))
                    {
                        strResponse = streamReader.ReadToEnd();
                    }
                }
            }
        }
catch (WebException exception)
{
    throw exception;
}
Score: 0

"As result it returns initial upload 14 page, not a result page with links i could 13 parse and present to a user..."

Maybe 12 that's just the behaviour of the upload 11 functionality: that after the upload has 10 finished, you can upload another file? I 9 think you have to call another url for the 8 "browse for file"-page (I suppose 7 that's the one you need).


Edit: Actually, if 6 the server sends a "redirect" (http 5 3xx), that's something the browser has to 4 handle, so if you're working with your own 3 client application in stead of a browser, you'll 2 have to implement redirection yourself. Here 1 the rfc for more information.

Score: 0

Try setting the maxRequestLength property 1 of the httpRuntime element in the Web.config.

Score: 0

In my case, duplicate filename causing the 8 issue as well. I save the file's settings 7 in an xml file but the name setting is duplicating 6 each other.

      <field name="StillImage">
        <prefix>isp_main_</prefix>
        <suffix>308</suffix>
        <width>1080</width>
        <height>1080</height>
      </field>
      <field name="ThumbnailImage">
        <prefix>isp_thumb_</prefix> // pay attention to this
        <suffix>308</suffix>
        <width>506</width>
        <height>506</height>
      </field>
      <field name="Logo">
        <prefix>isp_thumb_</prefix> // and this
        <suffix>306</suffix>
        <width>506</width>
        <height>506</height>
      </field>

And, in the other case I had, the issue is in the file length. Please 5 do check the allowed file size on your server. In 4 your script just do check this part :

dataStream.Write(filesBytesArray, 0, filesBytesArray.Length);
dataStream.Close();

And 3 if you dont know, just limit the file uploaded 2 size in your frontend section ie. HTML <input type="file"> upload 1 element, this is good reference for limiting file size and other filter.

More Related questions