上传multipart / form-data是随机传递失败的

时间:2017-10-19 09:16:14

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

我使用以下代码将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   dispatch @ 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只有一个带有上页的标签。 enter image description here

1 个答案:

答案 0 :(得分:0)

当在几毫秒内有很多请求时,服务器失败,没有人在服务器端响应。

摘要:服务器端故障。