[英]How can I handle HTTP error responses when using an iframe file upload?
I am using the following dirty workaround code to simulate an ajax file upload. 我正在使用以下肮脏的解决方法代码来模拟ajax文件上传。 This works fine, but when I set
maxAllowedContentLength
in web.config
, my iframe loads 'normally' but with an error message as content: 这可以正常工作,但是当我在
web.config
设置maxAllowedContentLength
时,我的iframe会“正常”加载,但内容包含错误消息:
dataAccess.submitAjaxPostFileRequest = function (completeFunction) {
$("#userProfileForm").get(0).setAttribute("action", $.acme.resource.links.editProfilePictureUrl);
var hasUploaded = false;
function uploadImageComplete() {
if (hasUploaded === true) {
return;
}
var responseObject = JSON.parse($("#upload_iframe").contents().find("pre")[0].innerText);
completeFunction(responseObject);
hasUploaded = true;
}
$("#upload_iframe").load(function() {
uploadImageComplete();
});
$("#userProfileForm")[0].submit();
};
In my Chrome console, I can see 在我的Chrome控制台中,我可以看到
POST http:/acmeHost:57810/Profile/UploadProfilePicture/ 404 (Not Found)
POST http:/ acmeHost:57810 / Profile / UploadProfilePicture / 404(未找到)
I would much prefer to detect this error response in my code over the risky business of parsing the iframe content and guessing there was an error. 与解析iframe内容并猜测存在错误的冒险工作相比,我更愿意在代码中检测到此错误响应。 For 'closer-to-home
errors, I have code that sends a json response, but for
maxAllowedContentLength`, IIS sends a 404.13 long before my code is ever hit. 对于“更接近家庭的
errors, I have code that sends a json response, but for
maxAllowedContentLength`,IIS在我的代码被击中之前就发送了404.13。
There is not much you can do if you have no control over the error. 如果您无法控制错误,则无能为力。 If the submission target is in the same domain than the submitter and you are not limited by the SOP, you can try to access the content of the iframe and figure out if it is showing a success message or an error message.
如果提交目标与提交者位于同一域中,并且您不受SOP的限制,则可以尝试访问iframe的内容,并确定它是否显示成功消息或错误消息。 However, this is a very bad strategy.
但是,这是一个非常糟糕的策略。
Why an IFRAME? 为什么要使用IFRAME? It is a pain.
真痛苦
If you want to upload files without the page flicking or transitioning, you can use the JS File API : File API File Upload - Read XMLHttpRequest in ASP.NET MVC 如果要上载文件而不会出现页面滑动或过渡的情况,则可以使用JS File API: File API File Upload-在ASP.NET MVC中读取XMLHttpRequest
The support is very good: http://caniuse.com/#feat=filereader 支持非常好: http : //caniuse.com/#feat=filereader
For old browsers that does not support the File API just provide a normal form POST. 对于不支持File API的旧浏览器,只需提供常规形式的POST。 Not pretty... but ok for old browsers.
不漂亮...但是对于旧浏览器来说还可以。
UPDATE 更新
Since there is no chance for you to use that API... Years ago I was in the same situation and the outcome was not straightforward. 由于您没有机会使用该API ...几年前,我处在相同的情况下,结果并不简单。 Basically, I created a upload ticket system where to upload a file you had to:
基本上,我创建了一个上传票证系统,可以在其中上传必须执行的文件:
POST /newupload/
, that would return a GUID. POST /newupload/
创建一个票证,它将返回一个GUID。 /newupload/dialog/<guid>
that would show the file submission form pointing to POST /newupload/<guid>/file
/newupload/dialog/<guid>
中创建一个iframe,该iframe将显示指向POST /newupload/<guid>/file
的文件提交表单 GET /newupload/guid/status
GET /newupload/guid/status
提供上传GET /newupload/guid/status
When I moved to the FileReader API... was a good day. 当我移至FileReader API时……真是美好的一天。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.