Big files uploading (WebException: The connection was closed unexpectedly)

后端 未结 4 751
广开言路
广开言路 2021-02-06 08:12

UPDATED

See post #3 below.

There is a need to upload a file to the web automatically (without browser). Host - Mini File Host v1.2 (if this does

相关标签:
4条回答
  • 2021-02-06 08:21

    Update : nope, there is no redirect.

    screenshot

    Read RFC2388 few times, rewrote the code and it finally worked (i guess the trouble was in utf-read trailing boundary instead of correct 7 bit ascii). Hooray? Nope :(. Only small files are transfered, big ones throwing "The 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 know that's a bug with .net stack and few solutions exists :

    1) increase both Timeout and ReadWriteTimeout of request

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

    3) set ProtocolVersion to 1.0 But neither one of them nor all of them altogether 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;
    }
    
    0 讨论(0)
  • 2021-02-06 08:34

    In my case, duplicate filename causing the issue as well. I save the file's settings in an xml file but the name setting is duplicating 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 do check the allowed file size on your server. In your script just do check this part :

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

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

    0 讨论(0)
  • 2021-02-06 08:43

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

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


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

    0 讨论(0)
  • 2021-02-06 08:43

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

    0 讨论(0)
提交回复
热议问题