【发布时间】:2020-01-26 04:47:20
【问题描述】:
这是我在 StackOverflow 上的第一篇文章,所以如果我的问题采用非常规格式,我提前道歉。我在 Intranet 站点上有一个表单,它允许用户通过将文件传递到服务器来上传文件。将文件传递到服务器的 js 函数如下所示。此代码在较小的文件(最大为 900kb)上运行完美,但较大的文件 (9mb) 会产生 500 错误甚至没有到达服务器。
function UploadDocument(customSectionId, evaluationSectionId) {
if ($('#eval-attachment-modal-file')[0].files != null) {
if (customSectionId != null && customSectionId == -1) {
customSectionId = null;
}
if (evaluationSectionId != null && evaluationSectionId == -1) {
evaluationSectionId = null;
}
var file = $('#eval-attachment-modal-file')[0].files[0];
if (file) {
var fd = new FormData();
fd.append("fileToUpload", file);
fd.append("evalId", EvalId);
fd.append("customSectionId", customSectionId);
fd.append("evaluationSectionId", evaluationSectionId);
fd.append("canUploadImages", @(Model.CanUploadImages ? "true" : "false"));
fd.append("canUploadDocuments", @(Model.CanUploadDocuments ? "true" : "false"));
$('#upload-file-button').hide();
$('#upload-file-spinner').show();
$.ajax({
url: '@Url.Action("UploadDocument")',
type: 'POST',
datatype: 'json',
data: fd,
processData: false,
contentType: false,
success: function (response) {
if (response != null && response.success) {
$('#evaluation-attachment-grid').trigger('reloadGrid');
if (evaluationSectionId == @((int) EvaluationSection.ExecutiveSignature)) {
var checkbox = $('#ExecutiveSignatureSignedOff');
var dateFieldForCheckbox = $('#ExecutiveSignatureSignedOffDate');
if (!checkbox.is(':checked')) {
checkbox.prop('checked', 'true');
dateFieldForCheckbox.val('@(DateTime.Now.ToShortDateString())');
SaveEvaluation(function () { AlertSuccess('Executive signature successfully uploaded. Evaluation saved.'); });
}
}
}
else {
if (response != null) {
if (response.errorMessages != null) {
for (var i = 0; i < response.errorMessages.length; i++) {
AlertError(response.errorMessages[i]);
}
}
}
else {
AlertError('Uploading Document Failed. Please contact your I.T. administrator.');
}
}
$('#upload-file-spinner').hide();
$('#upload-file-button').show();
},
async: true
});
}
}
}
在调试时,浏览器指出 jquery 在尝试发送 XMLHttpRequest 时发生错误(xhr.send 行是爆炸的那一行)
try {
// Do send the request (this may raise an exception)
xhr.send( options.hasContent && options.data || null );
} catch ( e ) {
// #14683: Only rethrow if this hasn't been notified as an error yet
if ( callback ) {
throw e;
}
}
我的一位同事表示,IIS 有时会在尝试将大文件传递到服务器时与您发生冲突,因此我将这个建议的代码添加到我的 Web 配置中无济于事
<system.webServer>
<security>
<requestFiltering>
<!-- maxAllowedContentLength is in bytes (B) -->
<requestLimits maxAllowedContentLength="20971520"/>
<!-- 20MB -->
</requestFiltering>
</security>
</system.webServer>
我怀疑这是 IIS 搞砸了我,但坦率地说,我没有想法。请告知 SO 社区
【问题讨论】:
-
您是否在 IIS 管理器中安装了该服务?
-
是的,但是请求永远不会到达服务器,这意味着服务永远不会参与错误。我可能应该从问题中删除那一点
标签: javascript jquery asp.net .net asp.net-mvc