【问题标题】:Uploading multipart/form-data is pass and fail randomly上传多部分/表单数据是随机通过和失败的
【发布时间】:2018-03-31 07:01:15
【问题描述】:

我使用以下代码将 multipart/form-data 上传到服务器:

<!DOCTYPE html>
    <html>
    <body>
    <form >
       <input  id="file" type="file" name="file" multiple><br>
    </form>
    <button id="ajax_submit">ajax_submit</button>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

    <script>
    $( "#ajax_submit" ).click(function()
    {
      //alert( "Handler for .click() called." );
      var i = 0;
      while (i < document.getElementById('file').files.length)
      {

        var data = new FormData();


    //alert("before" + i);
        data.append('file', document.getElementById('file').files[i]);//first string parameter indicate the input field to name for server side
    //alert("after" + i);

        jQuery.ajax({
          url: 'http://xx.xxx.xx.xxx:8080/sign',
          data: data,
          cache: false,
          contentType: false,
          processData: false,
          method: 'POST',
          type: 'POST', // For jQuery < 1.9
          success: function(data)
          {
            //alert(data);
          }
        });
        i++;
      }
    });
    </script>
    </body>
    </html>

问题是一次选择多个文件时,当我选择一个概率较高的文件时,获得成功响应的概率非常低。

当它获得成功时,我在 Chrome 上得到了以下 请求和响应标头

Genaral
    Request URL:http://xx.xxx.xx.xxx:8080/sign
    Request Method:POST
    Status Code:200 
    Remote Address:xx.xxx.xx.xxx:8080
    Referrer Policy:no-referrer-when-downgrade
Response Headers
    Date:Thu, 19 Oct 2017 08:50:08 GMT
    Transfer-Encoding:chunked
    X-Application-Context:application
Request Headers
    Accept:*/*
    Accept-Encoding:gzip, deflate
    Accept-Language:en-US,en;q=0.8
    Connection:keep-alive
    Content-Length:33274
    Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryOuscIdBAA52VbAZK
    Host:xx.xxx.xx.xxx:8080
    Origin:null
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
 Request Payload
    ------WebKitFormBoundaryOuscIdBAA52VbAZK
    Content-Disposition: form-data; name="file"; filename="text.pdf"
    Content-Type: application/pdf

当我失败时,我在 Safari 和 Chrome 浏览器上收到以下 错误消息请求和响应标头

铬:

Genaral    
    Request URL:http://xx.xxx.xx.xxx:8080/sign
    Referrer Policy:no-referrer-when-downgrade
Request Headers
    Provisional headers are shown
    Accept:*/*
    Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryAmGAPOT6r0Voe5mr
    Origin:null
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Request Payload
    ------WebKitFormBoundaryAmGAPOT6r0Voe5mr
    Content-Disposition: form-data; name="file"; filename="empty.pdf"
    Content-Type: application/pdf

POST http://xx.xxx.xx.xxx:8080/sign net::ERR_CONNECTION_REFUSED 发送@jquery-3.2.1.min.js:4 ajax@jquery-3.2.1.min.js:4 (匿名)@multiplefileHandler.html:26 调度@ jquery-3.2.1.min.js:3 q.handle @ jquery-3.2.1.min.js:4

POST http://xx.xxx.xx.xxx:8080/sign net::ERR_CONNECTION_RESET 发送@jquery-3.2.1.min.js:4 ajax@jquery-3.2.1.min.js:4 (匿名)@multiplefileHandler.html:26 dispatch@jquery-3.2.1.min.js:3 q.handle@jquery-3.2.1.min.js:3


Safari:

加载资源失败:网络连接丢失。

加载资源失败:无法连接到服务器。


当我尝试使用 Java Apache HTTP 客户端调用时,出现以下错误:

org.apache.http.NoHttpResponseException: xx.xxx.xx.xxx:8080 failed to respond

当我选择 8 个文件并使用 Chrome 按ajax_submit 按钮时的活动监视器屏幕截图。当时 Chrome 只有一个标签页。

【问题讨论】:

  • 虽然我很欣赏客户端错误,但我认为显示服务器日志对于调试此问题也很重要。 connection_refused 是一个服务器端错误,提示服务器本身没有连接

标签: javascript ajax apache-httpclient-4.x


【解决方案1】:

当几毫秒内有很多请求时,服务器出现故障,服务器端没有人响应。

总结:服务器端故障。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 2016-01-17
    • 2013-04-27
    • 1970-01-01
    相关资源
    最近更新 更多