javascript - How can I handle HTTP error responses when using an iframe file upload? -


i using following dirty workaround code simulate ajax file upload. works fine, when set maxallowedcontentlength in web.config, iframe loads 'normally' error message content:

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 chrome console, can see

post http:/acmehost:57810/profile/uploadprofilepicture/ 404 (not found)

i prefer detect error response in code on risky business of parsing iframe content , guessing there error. 'closer-to-homeerrors, have code sends json response, formaxallowedcontentlength`, iis sends 404.13 long before code ever hit.

there not can if have no control on error. if submission target in same domain submitter , not limited sop, can try access content of iframe , figure out if showing success message or error message. however, bad strategy.

why iframe? pain.

if want upload files without page flicking or transitioning, can use js file api : file api file upload - read xmlhttprequest in asp.net mvc

the support good: http://caniuse.com/#feat=filereader

for old browsers not support file api provide normal form post. not pretty... ok old browsers.

update

since there no chance use api... years ago in same situation , outcome not straightforward. basically, created upload ticket system upload file had to:

  • create ticket post /newupload/ , return guid.
  • create iframe /newupload/dialog/<guid> show file submission form pointing post /newupload/<guid>/file
  • serve upload status @ get /newupload/guid/status
  • check submitter (the iframe outer container) status of upload every 500ms.
  • when upload started, hide iframe or show fancy endless progress bar.
  • when upload operation completed of faulted, remove iframe , let user know how went.

when moved filereader api... day.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -