上传 multipart/form-data 随机通过和失败
Uploading multipart/form-data is pass and fail randomly
我使用以下代码将 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>
问题是当 select 一次有多个文件时,获得成功响应的概率非常低,而当我 select 一个文件时它有更高的概率。
当它成功时,我在Chrome上得到了请求和响应headers:
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
当我失败时,我得到了以下错误消息、请求和响应headers 在 Safari 和 Chrome 浏览器上:
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
send @ jquery-3.2.1.min.js:4 ajax @ jquery-3.2.1.min.js:4
(anonymous) @ 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
send @ jquery-3.2.1.min.js:4 ajax @ jquery-3.2.1.min.js:4
(anonymous) @ multiplefileHandler.html:26
dispatch @ jquery-3.2.1.min.js:3 q.handle @ jquery-3.2.1.min.js:3
野生动物园:
Failed to load resource: The network connection was lost.
Failed to load resource: Could not connect to the server.
当我尝试使用 Java Apache HTTP 客户端进行调用时,出现以下错误:
org.apache.http.NoHttpResponseException: xx.xxx.xx.xxx:8080 failed to respond
当我 select 8 个文件并使用 Chrome 按下 ajax_submit
按钮时,activity 监视器的屏幕截图。当时Chrome只有一个tab上面的页面。
当几毫秒内有很多请求时,服务器会失败,服务器端没有人响应。
总结:服务器端故障。
我使用以下代码将 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>
问题是当 select 一次有多个文件时,获得成功响应的概率非常低,而当我 select 一个文件时它有更高的概率。
当它成功时,我在Chrome上得到了请求和响应headers:
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
当我失败时,我得到了以下错误消息、请求和响应headers 在 Safari 和 Chrome 浏览器上:
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 send @ jquery-3.2.1.min.js:4 ajax @ jquery-3.2.1.min.js:4 (anonymous) @ 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 send @ jquery-3.2.1.min.js:4 ajax @ jquery-3.2.1.min.js:4 (anonymous) @ multiplefileHandler.html:26 dispatch @ jquery-3.2.1.min.js:3 q.handle @ jquery-3.2.1.min.js:3
野生动物园:
Failed to load resource: The network connection was lost.
Failed to load resource: Could not connect to the server.
当我尝试使用 Java Apache HTTP 客户端进行调用时,出现以下错误:
org.apache.http.NoHttpResponseException: xx.xxx.xx.xxx:8080 failed to respond
当我 select 8 个文件并使用 Chrome 按下 ajax_submit
按钮时,activity 监视器的屏幕截图。当时Chrome只有一个tab上面的页面。
当几毫秒内有很多请求时,服务器会失败,服务器端没有人响应。
总结:服务器端故障。