Fineuploadererror handling

我在MVC4应用程序中使用了fineuploader v3.9 UI和jquery包装器。 此时,我认为我应该只允许2.5MB的文件,由validatieon sizeLimit属性设置。 我目前的问题是,当我上传不受支持的文件类型或文件太大时,我会收到此错误弹出窗口:

在此处输入图像描述

error handling程序似乎没有触发,并且我的服务器端代码中没有命中断点。 话虽这么说,当我选择一个小文件时,我可以在我的控制器方法代码中的断点处停止并且一切正常(成功上传)。 任何关于文件太大或不允许上传的内容的任何线索?

这是我所有的fineuploader js代码。 我有处理’完整’和’提交’的事件,所以我可以在上传过程中正确启用/禁用按钮,这些就可以了。 但是不确定“错误”处理程序是什么。 我可以在UI jQuery中使用’qq’对象引用吗? 它没有在其他任何地方使用,所以我想知道这是否是我的问题? 思考?

// Uploader control setup var fineuploader = $('#files-upload').fineUploader({ request: { endpoint: '@Url.Action("UploadFile", "Survey")', customHeaders: { Accept: 'application/json' }, params: { surveyInstanceId: (function () { return instance; }), surveyItemResultId: (function () { return surveyItemResultId; }), itemId: (function () { return itemId; }), loopingIndex: (function () { return loopingCounter++; }) } }, validation: { acceptFiles: ['image/*','application/xls','application/pdf', 'text/csv', 'application/vnd.openxmlformats-officedocument.spreadsheetml.template','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-excel'] , allowedExtensions: ['jpeg', 'jpg', 'gif', 'png', 'bmp', 'csv', 'xls', 'xlsx', 'pdf', 'xlt', 'xltx', 'txt'], sizeLimit: 1024*1024*2.5, // 2.5MB stopOnFirstInvalidFile: false }, multiple: true, text: { //uploadButton: 'Select your upload file(s)' uploadButton: 'Select your upload file(s)' } }).on('submitted', function(event, id, filename) { alert('submitted ' + filename); filesToUpload++; $(':input[type=button], :input[type=submit], :input[type=reset]').attr('disabled', 'disabled'); }).on('complete', function(event, id, filename, responseJSON) { alert('completed ' + filename); uploadedFileCounter++; if (filesToUpload == uploadedFileCounter) { $(':input[type=button], :input[type=submit], :input[type=reset]').removeAttr('disabled'); } }).on('error', function(event,id, name, errorReason, xhr) { alert(qq.format("Error on file number {} - {}. Reason: {}", id, name, errorReason)); }); 

});

这是我在上传过程中遇到的服务器方法的shell:

  [HttpPost] public JsonResult UploadFile(HttpPostedFileWrapper qqfile, int surveyInstanceId, int surveyItemResultId, int itemId, int loopingIndex) { try { bool isValid = false; // MORE UPLOAD CODE if (isSaveValid && isResultItemEntryValid) isValid = true; return CreateJsonResult(isValid); } } 

我的方法返回JSON成功消息

  private JsonResult CreateJsonResult(bool isSuccess) { var json = new JsonResult(); json.ContentType = "text/plain"; json.Data = new { success = isSuccess }; return json; } 

这一切都可以在FireFox v24和Chrome v30中正常工作,但在IE 9中,在调试控制台中我看到了:

在此处输入图像描述

在IE中,提交和完成的事件触发,但上传失败。 在其他浏览器中,可以看到正确的最大大小超出错误,并且不会触发任何事件。

根据控制台消息,您的服务器正在返回某种无效响应。 请注意,在IE9及更早版本中未强制实施文件大小限制,因为无法在这些浏览器中确定客户端的文件大小。 您需要仔细查看网络流量,以了解服务器返回的内容。 如果在Fine Uploader中启用debug选项,则响应有效内容的内容将打印到javascript控制台。

当使用DEBUG时,我在IE中获得的错误是“超出最大请求长度”。

这可以解决(至少对我来说)是将以下值添加到我的项目的web.config中:system.web值以kbs为单位,因此这是1GB,作为示例

    

如果您在IIS中托管,请添加以下内容:maxAllowedContentLength以字节为单位。

        

现在,当我运行我的应用程序时,我的控制台输出不是错误,只是适当的FALSE返回。

 LOG: [FineUploader 3.9.0-3] Received 1 files or inputs. LOG: [FineUploader 3.9.0-3] Sending upload request for 0 LOG: [FineUploader 3.9.0-3] Received response for 0_874e4439-c5c0-4b95-970f-16eb1cf89405 LOG: [FineUploader 3.9.0-3] iframe loaded LOG: [FineUploader 3.9.0-3] converting iframe's innerHTML to JSON LOG: [FineUploader 3.9.0-3] innerHTML = 
{"success":false}

进一步更新这里是我的FineUploader代码的一小部分,显示了JSON返回中自定义错误消息集的’failedUploadTextDisplay’。

  validation: { acceptFiles: ['image/*','application/xls','application/pdf', 'text/csv', 'application/vnd.openxmlformats-officedocument.spreadsheetml.template','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-excel'] , allowedExtensions: ['jpeg', 'jpg', 'gif', 'png', 'bmp', 'csv', 'xls', 'xlsx', 'pdf', 'xlt', 'xltx', 'txt'], sizeLimit: 1024*1024*2.5, // 2.5MB stopOnFirstInvalidFile: false }, failedUploadTextDisplay: { mode: 'custom' }, 

这是FineUploader调用的服务器方法的一部分。 对于IE9及更早版本,客户端fileSizevalidation不会阻止代码返回服务器。 我已经添加了服务器端validation,我检查了contentlength,并调用我创建JSON返回的方法。

  [HttpPost] public JsonResult UploadFile(HttpPostedFileWrapper qqfile, int surveyInstanceId, int surveyItemResultId, int itemId, int loopingIndex) { try { bool isValid = false; // file is too big, throw error. if (qqfile.ContentLength > (1024*1024*2.5)) { return CreateJsonResult(false, "File size exceeded, max file is 2.5MB."); } 

这是创建JSON返回的方法。 如果出现错误,我还会使用我想要的任何自定义文本发回“错误”属性。

  private JsonResult CreateJsonResult(bool isSuccess, string response = "") { var json = new JsonResult(); json.ContentType = "text/plain"; if (!isSuccess) { json.Data = new { success = isSuccess, error = response }; } else { json.Data = new { success = isSuccess }; } return json; }