//tell browser program going to return an application file //instead of html page response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition","attachment;filename=temp.csv"); temp.csv可以改为任何的其他文件名
Content-Disposition header - forcing SaveAs in browsersThere are situations (to save a documentation in pdf format or a financial document) where you might want a hyperlink leading to a file to present a SaveAs dialog in browser. Ways to do that exist. However, flaky browser support makes its usage a walk in the mine field.
Forcing SaveAs using the HTTP headerIn 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=<file name.ext> Where <file name.ext> is the filename you want to appear in SaveAs dialog (like finances.xls or mortgage.pdf) - without < and > 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).