Forcing SaveAs using the HTTP header

Forcing SaveAs using the HTTP header

In order to force the browser to show SaveAs dialog when clicking a hyperlink you have to include the following header in HTTP response of the file to be downloaded:

Content-Disposition: attachment; filename=

Where is the filename you want to appear in SaveAs dialog (like finances.xls or mortgage.pdf) – without symbols.

You have to keep the following in mind:

  • The filename should be in US-ASCII charset.
  • The filename should not have any directory path information specified.
  • The filename should not be enclosed in double quotes even though most browsers will support it.
  • Content-Type header should be before Content-Disposition.
  • Content-Type header should refer to an unknown MIME type (at least until the older browsers go away).

There is something more about it, you must read


before you use this header.

0 Replies to “Forcing SaveAs using the HTTP header”

  1. Please note that this code will not give work properly on google chrome, though it works fine on IE.

    The problem on google Chrome is that the file name does not appears.

    I used

    response.setHeader(“Content-Disposition: “, “attachment; filename=” + targetFile.getName());

Leave a Reply