Solved

Force Google Chrome to Open 'Save As' Dialog Immediately

Posted on 2014-02-21
5
2,994 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 43

Accepted Solution

by:
Rob 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
This video teaches viewers about errors in exception handling.
Suggested Courses

632 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