The role and use of Content Disposition in header

  • 2020-05-17 04:57:05
  • OfStack

Content-disposition is an extension of the MIME protocol, which instructs the MIME user agent how to display additional files. Content-disposition can actually control the content requested by the user to be saved as a file and provide a default file name. The file can be displayed directly in the browser or the file download dialog box will pop up when the user accesses it.

Format description:
content-disposition = "Content-Disposition" ":" disposition-type *(";") disposition - parm)

Field description:
Content-Disposition is the property name
How is disposition-type downloaded? For example, attachment is downloaded as an attachment
disposition-parm is the file name when it is saved by default
When the server sends a file to the client browser, if it is a file type supported by the browser, 1 will usually be opened by the browser by default, such as txt, jpg, etc., which will be displayed directly in the browser. If you need to prompt the user to save, you should use Content-Disposition for 1 processing.
 
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.txt"); 

Note: this will prompt the browser to save or open, and if you choose to open it, you will open it using an associated program such as notepad instead of IE.
Content-Disposition provides a default file name when the user wants to save the requested content as a file. The specific definition is 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 ) 


Then we can know from the above specific examples:

Content-Disposition: attachment; filename="filename.xls"
Of course the filename parameter can contain path information, but User-Agnet ignores this information and only USES the last part of the path information as the file name. When you use this header with a response type of application/ octet-stream, it means that you don't want to display the content directly. Instead, a "file download" dialog pops up and it's up to you to decide whether to "open" or "save."

Matters needing attention:

1. When the code USES Content-Disposition to make sure the browser pops up the download dialog. response. addHeader (" Content - Disposition ", "attachment"); Make sure you don't do anything to disable browser caching. As follows:
 
response.setHeader("Pragma", "No-cache"); 
response.setHeader("Cache-Control", "No-cache"); 
response.setDateHeader("Expires", 0); 

Otherwise you'll find that the download function works fine under opera and firefox, but not under IE

Related articles: