views:

389

answers:

2

I have a modal window that presents a form to users. The form includes one or more file uploads as well. Therefore I cannot submit the form with ajax.

At the moment I'm doing a standard submit with a submit button. When the form is submitted (which takes a few seconds in general), a PDF is generated and sent back for download (i.e. the Content-Disposition: attachment header), to force the download. Once the upload of the form data has completed, I need to close the modal window. If I call the close method straight way the submission never completes and the file download is never triggered (I think this is because the modal window is removed from the DOM). I can set a timeout, but there could be n number of images, so the time it takes to upload is unpredictable (and relative to the number of uploads).

I've looked at the jQuery form plugin (http://jquery.malsup.com/form/#file-upload), but apparently it uses an iframe to upload files, so I'm not sure if it will achieve what I need.

Thanks for any help,

Dom.

+1  A: 

The Form plugin you mentioned should work - why do you suspect it won't?

nickf
I've used the Form plugin for uploads myself. Hidden iframe targeting is standard practice for "ajax" uploading.
bluej100
I debugged the form plugin, and I can get the callback working, however the modal is destroyed too fast, so the request never finishes and the file doesnt download
Dominic
@Dominic Can't you hide the modal instead of destroying it?
bluej100
A: 

Rewritten after rereading the question. Here's what I'd try:

  1. Serialize the form and make it the return value from the dialog, i.e. pass the data back to the page.
  2. Close the dialog
  3. Submit the serialized form
Isaac Cambron
The file is streamed, so I cant do a GET on the file. I know that you cant do this with ajax, which is why I was originally asking.I obviously understand the need to call close within the callback, it was more about whether there was a window or document event I can bind to when the file download begins. Ive tested the form plugin, but the post-submit callback never gets triggered. The pre-submit executes fine, the form submits fine, the file downloads, but nothing happens when after the form submits (i.e. no post-submit callback).
Dominic
Ah, reread the question and realized you were doing it as a synchronous POST. I guess I just skipped over the first sentence. And now I see why you wanted a timeout. Edited response.
Isaac Cambron
Cant serialize file objects :(
Dominic
Hmm, right. I give up
Isaac Cambron