This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14464|Reply: 1

http response header Content-disposition

[Copy link]
Posted on 10/14/2016 9:11:43 AM | | |
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);





Previous:C# large file copying reads and writes
Next:c# Calculate the SHA256 value of the file
 Landlord| Posted on 12/13/2024 6:29:16 PM |
Java oss Alibaba Cloud upload file settings download file name
https://www.itsvse.com/thread-7853-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com