47

I used the jQuery Form plugin for asynchronous form submission. For forms that contain files, it copies the form to a hidden iframe, submits it, and copies back the iframe's contents. The problem is that I can't figure out how to find what HTTP status code was returned by the server. For example, if the server returns 404, the data from the iframe will be copied as normal and treated as a regular response.

I've tried poking around in the iframe objects looking for some sort of status_code attribute, but haven't been able to find anything like that.


The $.ajax() function can't be used, because it does not support uploading files. The only way to asynchronously upload files that I know of is using the hidden iframe method.

2
  • 1
    why not run some js on the result iframe? what i mean is, you can set a timeout for the page load ( on the parent ), and the child (iframe) should set a value in the parent. if that value is not set until the timeout, the iframe failed. Commented May 24, 2011 at 12:25
  • ajax can be used if you read the file into a string first then just submit it like a regular old POST Commented Jun 22, 2018 at 5:16

2 Answers 2

19

You can't get page headers by JS, but you can distinguish error from success: Try something like this:

<script type="text/javascript">

    var uploadStarted = false;
    function OnUploadStart(){            
        uploadStarted = true;
    }

    function OnUploadComplete(state,message){       

       if(state == 1)
        alert("Success: "+message);     
       else
         if(state == 0 && uploadStarted)
            alert("Error:"+( message ? message : "unknow" ));
    }   

</script>


<iframe id="uploader" name="uploader" onload="OnUploadComplete(0)" style="width:0px;height:0px;border:none;"></iframe>

<form id="sender" action="/upload.php" method="post" target="uploader" enctype="multipart/form-data" onsubmit="OnUploadStart()">
<input type="file" name="files[upload]"/>
<input type="submit" value="Upload"/>
</form>

On server side:

/*
  file: upload.php
*/
<?php 

   // do some stuff with file       

  print '<script type="text/javascript">';
  if(success)
     print 'window.parent.OnUploadComplete(1,"File uploaded!");';
  else
     print 'window.parent.OnUploadComplete(0, "File too large!");';
  print  '</script>';
?>
Sign up to request clarification or add additional context in comments.

1 Comment

If the file is too large, php will 500, preventing your error message from being shown.
-4

You can't retrieving HTTP status code from loaded "iframe" directly. But when an http error occured, the server will returned nothing to the "iframe". So the iframe has not content. you can check the iframe body, when the body of iframe is blank, use ajax with the same url to get the response from server. Then you can retrieve the http status code from response.

2 Comments

Very wrong. This is just a common case, but not even a convention or good practice. HTTP servers usually should send an response body containing an error message.
In the case of using "iframe" to download a file, it's different with general HTTP request. In particular, IE9 will replace "iframe" body with local error page from client machine unless HTTP status is 200. Thus, when you try to get the body of iframe, you will get a cross-domain error. @Lothar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.