Today's browsers can recognize many file types (txt,pdf,jpg ...) and automatically open them in the browser
Situation One:
Add header information content-disposition "attachment;" Causes the browser to force the download:
Copy Code code as follows:
#表示浏览器内嵌显示一个文件
Content-disposition:inline; Filename=foobar.pdf
#表示会下载文件, such as the Firefox browser
Content-disposition:attachment; Filename=foobar.pdf
The Nginx configuration is as follows, adding the following location to the appropriate server:
Copy Code code as follows:
Location/upload {
Add_header content-disposition "attachment;";
}
Situation Two:
There is such a demand, for picture files and PDFs and other file links, as long as access is really a path, can not open the picture in the browser, but prompts the user to save to local, file name using access to the file name.
This problem is mainly caused by IE, no matter what MIME type, such as manually set the MIME type of the image to Octet-stream, if the browser to know the file suffix, still open the picture in the browser, ie browser enough rubbish?
Solution:
In response to the HTTP header add: content-disposition:attachment; Filename= file name
The Nginx configuration is as follows:
Copy Code code as follows:
Location ~ ^/somepath/(. *) $ {
Add_header content-disposition "attachment; Filename=$1 ";
Alias "E:/apache-tomcat-7.0.32/webapps/upload/$1";
}
A regular expression is used here to capture the file name of the request.
In addition, need to pay attention to Nginx location priority, First is =, then is ^~, finally is ~.
In this way, IE browser will ignore the original MIME type, prompting the user to save the file to the local.