Usage of Content-disposition in phpheader

Source: Internet
Author: User
Content-disposition is an extension of the MIME protocol. The MIME protocol instructs the MIME user agent how to display additional files. Content-disposition can actually control user requests

Content-disposition is an extension of the MIME protocol. The MIME protocol instructs the MIME user agent how to display additional files. Content-disposition can actually control whether the Content requested by the user is saved as a file and provide a default file name. The file is displayed directly in the browser or a file download dialog box is displayed during access.

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

Field description:

Content-Disposition: attribute name

How is disposition-type downloaded, for example, attachment?

Disposition-parm is the default file name for saving

When the server sends a file to the client browser, if the file type is supported by the browser, it is generally opened by default using a browser, such as txt and jpg, and will be directly displayed in the browser, if you need to prompt the user to save, use Content-Disposition for processing. The key is to add attachment:

Response. AppendHeader ("Content-Disposition", "attachment?filename=filename.txt ");

Note: In this case, the browser will prompt whether to save or open the file. even if you choose to open the file, it will also use the associated program, such as Notepad, instead of opening the file directly by IE.

Content-Disposition provides a default file name when you want to save the requested Content as a file. The specific definition is as follows:

  1. Content-disposition = "Content-Disposition" ":"
  2. Disposition-type * (";" disposition-parm)
  3. Disposition-type = "attachment" | disp-extension-token
  4. Disposition-parm = filename-parm | disp-extension-parm
  5. Filename-parm = "filename" "=" quoted-string
  6. Disp-extension-token = token
  7. Disp-extension-parm = token "=" (token | quoted-string)

Then we can see the specific example: 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. If you use this header when the response type is application/octet-stream, it means that you do not want to directly display the content, but instead bring up a "file download" dialog box, the next step is to decide whether to "open" or "save.

Note:1. when Content-Disposition is used in the code to ensure that the download dialog box is displayed in the browser, response. addHeader ("Content-Disposition", "attachment"); make sure that you have not performed any operations to prohibit browser caching. As follows:

  1. Response. setHeader ("Pragma", "No-cache ");
  2. Response. setHeader ("Cache-Control", "No-cache ");
  3. Response. setDateHeader ("Expires", 0 );

Otherwise, you will find that the download function is okay in opera and firefox, but not in IE.

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.