Solved

Force Google Chrome to Open 'Save As' Dialog Immediately

Posted on 2014-02-21
5
2,805 Views
Last Modified: 2014-02-24
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
Comment
Question by:WalterRR
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39879268
what about opening a popup with a cool message which after second will redirect to the file?
0
 
LVL 33

Assisted Solution

by:shalomc
shalomc earned 250 total points
ID: 39879434
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
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 250 total points
ID: 39881172
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
 
LVL 33

Expert Comment

by:shalomc
ID: 39881287
I like the "Content-Disposition: attachment " header better, but suit yourself to whichever method works for you.
0
 

Author Closing Comment

by:WalterRR
ID: 39882659
Many thanks for the help guys........
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Easy responsive table out of existing table 28 55
jboss wildfly 10.1 10 97
Scroll 5 news at a time. 4 28
Diminish Pop-up  in 3 seconds 7 22
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question