The function and use of content-disposition in header _php tutorial

Source: Internet
Author: User
Content-disposition is an extension of the MIME protocol, and the MIME protocol indicates how the MIME user agent displays additional files. Content-disposition can actually control when the user requests the content to save as a file, provide a default file name, the file is displayed directly in the browser or the File Download dialog box when accessed.

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

Field Description:
Content-disposition is a property name
How Disposition-type is downloaded, such as attachment for download by 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 browsers, it will usually be opened by default browser, such as txt, JPG, etc., will be displayed directly in the browser, if you need to prompt the user to save, we should use content-disposition to do a bit of processing, The key is to be sure to add attachment:
Copy CodeThe code is as follows:
Response.appendheader ("Content-disposition", "attachment;filename=filename.txt");

Note: This will prompt the browser to save or open, even if you choose to open, will also use the associated program such as Notepad open, instead of IE directly opened.
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:
Copy CodeThe code 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)


so the concrete examples are:

Content-disposition:attachment; Filename= "Filename.xls"
Of course, the filename parameter can contain path information, but user-agnet ignores the information and only makes the last part of the path information the file name. When you use this header message in response type Application/octet-stream, it means that 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. When the code uses Content-disposition to ensure that the browser pops up the download dialog box. Response.AddHeader ("Content-disposition", "attachment"); Make sure you haven't done anything about prohibiting browser caching. As follows:
Copy CodeThe code is as follows:
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);

Otherwise you will find that the download function in opera and Firefox is good, under IE is not OK

http://www.bkjia.com/PHPjc/325564.html www.bkjia.com true http://www.bkjia.com/PHPjc/325564.html techarticle Content-disposition is an extension of the MIME protocol, and the MIME protocol indicates how the MIME user agent displays additional files. Content-disposition can actually control the contents of the user request to save as a ...

  • Related Article

    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.