简体   繁体   English

使用iframe文件上传时,如何处理HTTP错误响应?

[英]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: 基本上,我创建了一个上传票证系统,可以在其中上传必须执行的文件:

  • create a ticket from POST /newupload/ , that would return a GUID. POST /newupload/创建一个票证,它将返回一个GUID。
  • create an iframe to /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的文件提交表单
  • serve the upload status at GET /newupload/guid/status GET /newupload/guid/status提供上传GET /newupload/guid/status
  • check from the submitter (the iframe outer container) the status of the upload every 500ms. 每隔500毫秒从提交者(iframe外部容器)检查上传状态。
  • when upload is started, hide the iframe or show something fancy like an endless progress bar. 开始上传后,请隐藏iframe或显示一些奇妙的内容,例如无休止的进度条。
  • when the upload operation is completed of faulted, remove iframe and let the user know how it went. 当上传操作失败时,请移除iframe,并告知用户进展情况。

When I moved to the FileReader API... was a good day. 当我移至FileReader API时……真是美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Angular 中使用 mergeMap 时,如何错误处理链接的 http 请求? - How can I error handle chained http requests individually when using mergeMap in Angular? 谁能建议如何处理我在使用调用 Post 并通过 protractor 获取 Http 方法时遇到的错误? - Can anyone suggest how to handle the error which i was facing when using to call an Post and get Http methods through protractor? 处理成功/错误响应http请求angularjs - handle success/error responses http request angularjs 如何使用nodejs创建一个http文件上传服务器? 不使用表格 - How can I make a http file upload server with nodejs? not using form 如何在加载iframe时处理错误? - How can I handle errors in loading an iframe? 如何使用iframe取消文件上传 - How to cancel a file upload using an iframe Casperjs:我如何打印http请求和响应? - Casperjs: How can I print http requests and responses? 在AngularJS中,如何在`$ http`的所有响应上应用检查功能? - In AngularJS, how can I apply a check function on all the responses of `$http`? 使用http访问我的Facebook iframe页面时,如何获得模态而不是弹出窗口? - How can I get a Modal rather than a pop up when using http to access my facebook iframe page? 使用iframe.src下载文件时如何获取返回状态码? - how can I get the return status code when using iframe.src to download a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM