The function and using method of Content-disposition in header _php skill

Source: Internet
Author: User
Content-disposition is an extension of the MIME protocol, which indicates how the MIME user agent displays the attached file. Content-disposition In fact, you can control the content of the user request to save as a file when a default file name is provided, the file is directly displayed on the browser or in the Access pop-up file Download dialog box.

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

Field Description:
Content-disposition as Property name
Disposition-type is the way to download, such as attachment as an attachment to download
Disposition-parm is the name of the default save
When the server sends a file to the client-side browser, if it is the type of file that is supported by browsers, it will usually be opened by default browser, such as txt, JPG, etc., will be directly displayed in the browser, if you need to prompt the user to save, you must use content-disposition for processing, The key is to add attachment:
Copy Code code as follows:

Response.appendheader ("Content-disposition", "attachment;filename=filename.txt");

Note: So the browser prompts to save or open, even if you choose to open, you will use the associated program such as Notepad to open, rather than ie directly open.
Content-disposition is to provide a default filename when the user wants to save the requested content as a file. The specific definition is as follows:
Copy Code code 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 there are concrete examples:

Content-disposition:attachment; Filename= "Filename.xls"
Of course the filename parameter can contain path information, but user-agnet ignores this information and only makes the last part of the path information the filename. When you use this header information in response type Application/octet-stream, that means you don't want to display the content directly, but instead pop up a "File download" dialog box, and then you decide whether to "open" or "save".

Precautions :

1. Use content-disposition in your code to make sure that the browser pops up the download dialog box. Response.AddHeader ("Content-disposition", "attachment") make sure you haven't done anything about preventing browser caching. As follows:
Copy Code code as follows:

Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);

Otherwise will find the download function in opera and Firefox inside Good no problem, in IE below is not

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.