Content-disposition is an extension of the MIME protocol, which instructs the MIME user agent to display attached files. When Internet Explorer receives the header, it activates the file download dialog box, and its file name box is automatically populated with the file name specified in the header. (Note that this is caused by design; You can't use this feature to save a document to a user's computer without asking the user where to save it. )
When the server sends a file to the client browser, if it is a file type supported by the browser, it will generally be opened by default using the browser, such as txt, jpg, etc., which will be displayed directly in the browser, if the user needs to be prompted to save, it is necessary to use Content-Disposition to process it, the key is to add attachment:
Response.AppendHeader("Content-Disposition","attachment; filename=FileName.txt");
Note: This will prompt you to save or open, and even if you choose to open it, it will open it with the associated program such as Notepad, instead of IE opening it directly.
Content-Disposition is to provide a default file name when the user wants to save the requested content as a file. The specific definitions are as follows:
content-disposition = “Content-Disposition” “:”
disposition-type *( “;” disposition-parm )
disposition-type = “attachment” | disp-extension-token
disposition-parm = filename-parm | disp-extension-parm
filename-parm = “filename” “=” quoted-string
disp-extension-token = token
disp-extension-parm = token “=” ( token | quoted-string )
So we can see specific examples from the above:
Cache-Control:private
Content-Disposition:attachment; filename=Index1.png
Content-Length:328462
Content-Type:image/png
Date:Fri, 14 Oct 2016 01:08:04 GMT
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:5.2
X-Powered-By:ASP.NET
Content-Disposition: attachment; filename=“filename.xls”
Of course, the filename parameter can contain path information, but User-Agnet will ignore this information and only use the last part of the path information as the file name. If you use this header in the response type application/octet-stream, it means that you don't want to display the content directly, but a "file download" dialog will pop up, and then it's up to you to decide whether to "open" or "save".
Notes:
1. When using Content-Disposition in the code to ensure that the browser pops up the download dialog. response.addHeader("Content-Disposition","attachment"); Make sure you haven't done anything about disabling browser caching. As follows:
response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "No-cache"); response.setDateHeader("Expires", 0);
|