views:

45

answers:

2

Hi all,

Basically, I am using an iframe to download a file. I set the source of the iframe to the file I want to download. That problem isn't that I can't get a download dialog. The problem is I can't close the window after the download is started.

What I was thinking is I could send back one file with this header.

"Content-Disposition","attachment;filename=test.txt"

Then I would like to send back another HTML for the browser to display. The whole point of the second file is to close the browse window.

BTW: It is far to late to go away from popups.

A: 

The whole point of the second file is to close the browse window.

You don't need this. Just setting that header on the response with the to-be-downloaded file is more than sufficient to get a Save As dialogue. You don't need to open the link in a new/blank window. Just let the link/form point to the desired URL and it'll open the Save As dialogue. The currently opened page will remain the same.

BalusC
I need to close the window, also, after the file download. Basically, I could use window.close, but I don't know when to call it. I need to wait for the response. That is why I was hoping I could send back to files at once. One for for download, and one with JavaScrip in it to close the window.
Grae
A: 

No, you can't send both a file and a page in the same response.

If you want two responses, you have to send two requests.

Alternatively, you can start the download, then use a timeout to close the window after the download has started:

window.location.href = 'test.txt';
window.setTimeout(function(){
  window.close();
}, 100);
Guffa
I need to send two files, one for download, one for display. I think a response can do this, because sometimes the browser downloads the picture at the same time and such things. I don't know much about multipart responses.
Grae
The timeout is ok for some files, but what if the file it large, and takes a few minutes to download? Is there a way around this?
Grae
@Grae: The download should contine even if you close the window. When you set the `href` it doesn't take effect until you return from the script, so the timeout is there so that the `href` can take effect before you close the window.
Guffa