Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3184
  • Last Modified:

Force Google Chrome to Open 'Save As' Dialog Immediately

I have a java servlet that returns a large pdf file to UI/browser for download. It takes the server 10-20 seconds to generate the file so I am trying to pop up the save as dialog ASAP to avoid browser lag, user confusion, wait and so on.

I only want to send headers etc. just to make connection, when server has its work done we flush the rest of payload.

I send <?xml XXXXXXXXXXXXXX?> immediately after headers and flush buffer. this works well in firefox and the browser shows the dialog thus eliminating the browser wait,,, this does not work for chrome.

Any help appreciated.
0
WalterRR
Asked:
WalterRR
2 Solutions
 
leakim971PluritechnicianCommented:
what about opening a popup with a cool message which after second will redirect to the file?
0
 
shalomcCommented:
You need to send the Content-Disposition http header, like in this php example.

$path = "path/to/file.pdf";
$filename = "file.pdf";
header('Content-Transfer-Encoding: binary');  
header('Accept-Ranges: bytes');  
header('Content-Length: ' . filesize($path));  
header('Content-Encoding: none');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' . $filename);  
readfile($path);  

Of course, java being java, it is a bit more complex in your case.
See this reference for an example how to add custome http headers in Java
http://vangjee.wordpress.com/2009/02/25/how-to-modify-request-headers-in-a-j2ee-web-application/
0
 
RobOwner (Aidellio)Commented:
Shalomc is close but if you specify the following headers the browser will not try to display the file but will prompt the user to download it:

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
0
 
shalomcCommented:
I like the "Content-Disposition: attachment " header better, but suit yourself to whichever method works for you.
0
 
WalterRRAuthor Commented:
Many thanks for the help guys........
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now