1

I have a form with upload

<form action="../upload" method="post" id="upload_form" enctype="multipart/form-data" target="upload_frame" >
        <input name="file" type="file" />
    </form>

and it's submit using javascript form another form submit button

function upload(){
    var uploadForm = document.getElementById('upload_form');

    uploadForm.submit();
    console.log('this should be called when the form finishes upload and respone is committed');

}

please tell if the example is not clear enough

  • @Yve I understood it as "I want to know when the server has received the request and responded". In other words, it looks like the OP is looking for a way to determine when the file has been sent to the server by checking for a response client-side. – Ray Nicholus Jul 07 '13 at 14:08

1 Answers1

3

Add an onload handler to your iframe. It will be called after the server responds to the request.

For example:

var uploadFrame = document.getElementsByName('upload_frame')[0];

function handleResponseReceived() {
    console.log('this should be called when the form finishes upload and respone is committed');
}

if (uploadFrame.addEventListener) {
    uploadFrame.addEventListener('load', handleResponseReceived, false);
}
else {
    uploadFrame.attachEvent('onload', handleResponseReceived);
}
Ray Nicholus
  • 19,538
  • 14
  • 59
  • 82
  • Not by me, but I don't see an iframe – mishik Jul 07 '13 at 13:42
  • This is not a good solution. You cannot get HTTP status of a loaded iframe, so you cannot handle responses properly. However, you answer DOES resolve the problem. But not in an elegant way. – igorpavlov Jul 07 '13 at 13:49
  • @igorpavlov Who said anything about needing HTTP status? This is actually the ONLY way to upload files without redirecting the parent window if the UA does not support the File API. It is common for a convention to be in place between client and server as far as the response body is concerned. This is how the server can communicate status back to the client in this situation, if needed. – Ray Nicholus Jul 07 '13 at 13:55
  • actually I don't need the answer anymore as I found it's all a disaster to upload a file inside JSF form with primefaces controls and validation lifecycle, but this is the answer for the rest of the visitors – Mohamed Wagdy Khorshid Jul 07 '13 at 19:47