Javascript 在第一次请求上传文件后,使用xhr ajax和formData发送其他请求
我花了很多时间试图在本地主机上模拟此错误,但没有成功。此错误有时发生在web的实时版本上。从web的实时版本发送错误电子邮件 这是一个错误Javascript 在第一次请求上传文件后,使用xhr ajax和formData发送其他请求,javascript,asp.net,ajax,asp.net-mvc,file-upload,Javascript,Asp.net,Ajax,Asp.net Mvc,File Upload,我花了很多时间试图在本地主机上模拟此错误,但没有成功。此错误有时发生在web的实时版本上。从web的实时版本发送错误电子邮件 这是一个错误 The parameters dictionary contains a null entry for parameter 'isJsonResponse' of non-nullable type 'System.Boolean' for method 'System.Web.Mvc.ActionResult UploadFileDocument(Mode
The parameters dictionary contains a null entry for parameter 'isJsonResponse' of non-nullable type 'System.Boolean' for method 'System.Web.Mvc.ActionResult UploadFileDocument(Model, Boolean)' in Controller'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
下面是javascript:
$('#uploadFile').click(function () {
var input = $('#file')[0];
if (input.files.length == 0) return;
$(".titleRed", $('#files')).remove();
var dObject = new Object();
dObject.TypeID = $('#types').val();
dObject.FileName = input.files[0].name;
dObject.File = input.files[0];
dObject.ID = '@HttpUtility.JavaScriptStringEncode(Model.ID)';
var formData = new FormData();
for (var key in dObject) {
formData.append(key, dObject[key]);
}
formData.append('isJsonResponse', false);
uploadFiles(formData, function (result) {
if (typeof result == 'object') {
console.log(result.msg);
} else {
$('#files').append(result);
$('#fileAlert').remove();
}
});
document.getElementById('file').value = '';
$('#types').val('1');
return false;
});
函数上载文件:
function uploadFiles(formData, updateCallback,progressCallback,startCallback,errorCallback) {
var xhr = new XMLHttpRequest();
xhr.open('POST', _uploadFilesURL);
//listner on progress (%) for progress bar
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
if (typeof progressCallback != "undefined") {
//count %
var percentComplete = evt.loaded / evt.total;
percentComplete = parseInt(percentComplete * 100);
debugLog(percentComplete);
progressCallback(percentComplete, this.progressId);
}
}
}, false);
// when downloading start create unique ID for progress bar
xhr.upload.addEventListener("loadstart", function (e) {
if (typeof startCallback != "undefined") {
// generating uniqe ID (GUID)
this.progressId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : r & 0x3 | 0x8; return v.toString(16); });
startCallback(this.progressId);
}
});
//callback on final result of request
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var data = null;
//if crash JSON.Parse then is possible that result is pure text(partialView)
try {
data = JSON.parse(xhr.responseText);
} catch (e) {
}
if (data == null) {
//supose it is pure text if json parse crash
updateCallback(xhr.responseText);
} else {
updateCallback(data.result);
}
} else if (xhr.readyState == 4 && xhr.status != 200) {
if (typeof errorCallback != "undefined") {
errorCallback(xhr);
}
}
}
xhr.send(formData);
}
以下是Asp net MVC方法代码:
[HttpPost]
public ActionResult UploadFile(Model mode, bool isJsonResponse)
{
型号:
public class Model
{
public string FileName { get; set; }
public int TypeID { get; set; }
public int? ModelID { get; set; }
public HttpPostedFileBase File { get; set; }
public int ID { get; set; }
}
该文件已正常上载。但在此之后,有时会发送另一个请求,但没有fileBody和其他参数,如isJsonResponse。文章只包含TypeID和FileName
错误来自IE、FF、Chrome
有人有这方面的经验吗?或者类似的东西好的,所以在使用应用程序笨拙进行另一次测试之后,我最终模拟了该行为。当互联网连接不稳定时,我会释放这种情况。好的,所以在使用application Clumbsy进行另一次测试后,我最终模拟了这种行为。当网络连接不稳定时,我会释放这种情况