Jump to content

Open a popup, download file, close popup?


Jessica

Recommended Posts

I have a page which is served via ajax. Depending on the user's selections, the page might present an excel or pdf file to download and save. So far the best way I've found to do it is have the ajax page load some JS to open a popup and that page presents the appropriate headers, since the page being loaded into a <div> on the existing page can't send the file correctly.

 

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xls");
header('Cache-Control: max-age=0');

 

So how can I get the popup to close automatically? It does in Firefox but the company uses IE6. I can't send HTML or JS to the popup since the headers specify it's an excel file - is there any way to close it?

 

Or any tips on a better way to do this?

Link to comment
Share on other sites

Do you want the user to be able to download a pdf file and save it? if so why not just put it up and link to it. Like.

 

www.mywebsite.co.uk/pdf/mynewsletter.pdf

 

When someone goes to this link they will (assuming the have the adobe software installed) be able to save it. The same goes for a word or excel document.

 

Desmond.

 

Link to comment
Share on other sites

Do you want the user to be able to download a pdf file and save it? if so why not just put it up and link to it. Like.

 

www.mywebsite.co.uk/pdf/mynewsletter.pdf

 

When someone goes to this link they will (assuming the have the adobe software installed) be able to save it. The same goes for a word or excel document.

 

Desmond.

 

 

The file is generated by a PHP script, it is not a static file.

Link to comment
Share on other sites

I assume that the company has security more or less locked down... But assuming they dont, you can use the javascript close.window method.

 

Can you explain how to do that after the content headers I specified are shown?

 

The popup contains the PHP page which generates the file, and sends it to the browser using those headers. How then do I print javascript to the browser?

 

Or do you mean using the original page? I can see that working but I wonder if it will block the download dialog. IE6 is mandatory.

Link to comment
Share on other sites

close.window is javascript. Output this with the buffer.

 

I did, it doesn't close the window. Although I think you must mean window.close() unless you're using some other code?

 

What I'm asking is why would you send javascript after an excel file contents? I mean, it makes no sense to me that it would work, the content type isn't text/html. How does

 

"header('Content-Type: application/vnd.ms-excel');" allow you to include javascript?

 

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$this->title.'.xls"');
header('Cache-Control: max-age=0');

//PHP to echo Excel output here.

echo '<script type="text/javascript">
window.close();
</script>';

 

It looks to me there's not a way to accomplish it this route.

 

One option I can do is generate the file and save it on the server before displaying the link to download it, but then if they didn't want to download it I'd have to delete it later.

 

The page displayed via ajax contains several options, the download is only one, and we only really need to generate the file if they request it. So that's why it's going to a popup. I don't want them to click the link, to generate the file and then give them another download link. That'd be annoying and counter-intuitive to the user.

 

Anyone else have any ideas?

Link to comment
Share on other sites

Why does it need to be a popup? You could easily just redirect them to a PHP page that generates the file. I've used this method before, and it doesn't actually take you away from the current web page.

 

I.e. it won't send you to a blank page when the file is being downloaded. As soon as the page is redirected or a link is clicked, the download will begin and the user will remain on their current page. This eliminates the need for javascript if you use a PHP header re-direct.

Link to comment
Share on other sites

Why does it need to be a popup? You could easily just redirect them to a PHP page that generates the file. I've used this method before, and it doesn't actually take you away from the current web page.

 

I.e. it won't send you to a blank page when the file is being downloaded. As soon as the page is redirected or a link is clicked, the download will begin and the user will remain on their current page. This eliminates the need for javascript if you use a PHP header re-direct.

 

Do you think this will work with an AJAX page? The page that would have the do the redirect is loaded only via AJAX. That is why I used a popup? I'll try to do what you suggest later today but I don't understand how it will work...

Link to comment
Share on other sites

It should work just fine. When an AJAX process has completed, the content becomes a regular web page and it could be treated as such.

 

I.e. the user opts to download the excel file, you validate the input etc. and if all is okay, you can do a PHP header re-direct to another PHP page that actually generates the file. The download begins and the user still remains on the same web page.

Link to comment
Share on other sites

I.e. the user opts to download the excel file, you validate the input etc. and if all is okay, you can do a PHP header re-direct to another PHP page that actually generates the file. The download begins and the user still remains on the same web page.

 

That's where I'm confused, I didn't think you could do a PHP header on a page like that, where it's inside existing HTML.

 

I put this in the page being returned via AJAX

header('Loction: http://google.com');
print 'test';
die();

 

And my div gets populated with

test
.

 

I guess I'm not explaining it correctly, since no one seems to get the problem....

Link to comment
Share on other sites

You could always use a Javascript re-direct in that case since you're using Javascript for AJAX anyway.

 

Add a callback... once the AJAX request has been processed, proceed to re-direct the user to the page which will generate the file.

Link to comment
Share on other sites

You could always use a Javascript re-direct in that case since you're using Javascript for AJAX anyway.

 

Add a callback... once the AJAX request has been processed, proceed to re-direct the user to the page which will generate the file.

 

That will take them off of the current page though, correct?

Link to comment
Share on other sites

No, well at least it shouldn't do. It might seem like it, but it shouldn't.

 

The only way to find out would be to test it. I don't use an automatic redirect, I actually use a link which a user clicks. Once clicked, the download begins and the current page remains unchanged. I don't see any reason why using an automatic process would be different.

Link to comment
Share on other sites

No, well at least it shouldn't do. It might seem like it, but it shouldn't.

 

The only way to find out would be to test it. I don't use an automatic redirect, I actually use a link which a user clicks. Once clicked, the download begins and the current page remains unchanged. I don't see any reason why using an automatic process would be different.

 

Just out of curiosity, you've done this for IE too right?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.