我使用jQuery上传随机生成的有效负载:
var promise = new jQuery.Deferred();
var uploadStartTime;
$.ajax({
url: '/Api/HeartBeat/UploadSpeedTest',
data: { '': self.payload },
contentType: "application/json; charset=utf-8",
timeout: 10000,
type: 'POST',
beforeSend: function () {
uploadStartTime = new Date();
},
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
console.log(percentComplete);
}
}, false);
return xhr;
},
}).done(function (e) {
var diff = Math.abs((new Date()) - uploadStartTime);
var speed = Math.round(e.AmountReceived / diff);
console.log('upload test successful. Payload: ' + e.AmountReceived + 'B. Required speed: ' + e.RequiredUploadSpeed + 'KB/s. Download time: ' + diff + 'ms. Actual speed: ' + speed + 'KB/s');
});
其中self.payload
是100kb生成的随机,base64编码的字符串。
当我运行此操作时,进度立即为100%,evt.loaded
和evt.total
相等。当我模拟与Fiddlr的5kb / s连接时,它将以100%挂起20秒并且永远不会进展。
这是Chrome中的错误还是我的实施错误?
这些是发送的标题:
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,nl;q=0.6
Connection:keep-alive
Content-Length:99937
Content-Type:application/json; charset=UTF-8
Host:host.domain.com
Origin:http://host.domain.com
Referer:http://host.domain.com/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36
X-Requested-With:XMLHttpRequest
答案 0 :(得分:0)
试试这个(模式)
// progressObj = {"now" : "", "then" : ""};
// add other metrics / properties to `progressObj`
$(document)
.on("ajaxStart", function () {
console.log("starting at " + $.now())
// progressObj.now = $.now();
// process other metrics / properties of `progressObj`
})
.on("ajaxStop", function () {
console.log("done at " + $.now());
// progressObj.then = $.now();
// process other metrics / properties of `progressObj`
// console.log(progressObj)
});