The Content-Disposition response-header field has been proposed as a means for the origin server to suggest a default filename if the user requests that the content is saved to a file.
Download mp3 files instead of playing them directly
Here is my original link: http://zcoder.cn/2015/01/01/Download mp3 files instead of playing them directly/
This article is suitable for everyone
Web engineers are suitable to read this article. Since I work in Java, the following codes are based on Java, but I will explain the basic principles
Requirements
In the browser, open a url whose output is an MP3 file stream. The requirement is to download it directly. However, there is a browser with its own playback function, and the browser will open the file directly.
What is Content-Disposition?, Content-disposition is an extension of the MIME protocol. The MIME protocol instructs the MIME user agent how to display attached files. Content-disposition can specify the file name when the user downloads it. In actual use, although Content-disposition can only specify a name, the browser will download it directly by default without specifying Content-Type; but if you do not specify Content-disposition, you can also download it by directly setting the response header Content-Type: application/octet-stream, but this The download does not specify a file name, and the file name downloaded by the user is the request path (Request URL).
The problem is simple at this time. We only need to add a filter and set the response header Content-Type: application/octet-stream for the required request address.
Others
In the process of solving this problem, I found that there are many answers on stackoverflow.com, but I only found two pieces of information using Baidu, and the writing was not good. This shows two problems: 1. Baidu has 2. There is no such environment in China. Even if everyone solves the problem, they do not pay attention to accumulation or sharing.
The following is the answer on stackoverflow.com, it is very good, please share it.
http://tools.ietf.org/html/rfc2616#section-19.5.1
Download mp3 files instead of playing them directly
Here is my original link: http://zcoder.cn/2015/01/01/Download mp3 files instead of playing them directly/
This article is suitable for everyone
Web engineers are suitable to read this article. Since I work in
Java
, the following codes are based onJava
, but I will explain the basic principlesRequirements
In the browser, open a
url
whose output is an MP3 file stream. The requirement is to download it directly. However, there is a browser with its own playback function, and the browser will open the file directly.Solution
Content-Disposition
rfc2616#section-19.5.1: http://tools.ietf.org/html/rfc2616#section-19.5.1
WIKI: http://en.wikipedia.org/wiki/MIME#Content-Disposition
What is Content-Disposition?,
Content-disposition
is an extension of the MIME protocol. The MIME protocol instructs the MIME user agent how to display attached files.Content-disposition
can specify the file name when the user downloads it. In actual use, althoughContent-disposition
can only specify a name, the browser will download it directly by default without specifyingContent-Type
; but if you do not specifyContent-disposition
, you can also download it by directly setting the response headerContent-Type: application/octet-stream
, but this The download does not specify a file name, and the file name downloaded by the user is the request path (Request URL).Set response-header (response header)
Java code example
If the request path is a static file
The problem is simple at this time. We only need to add a filter and set the response header
Content-Type: application/octet-stream
for the required request address.Others
In the process of solving this problem, I found that there are many answers on stackoverflow.com, but I only found two pieces of information using Baidu, and the writing was not good. This shows two problems: 1. Baidu has 2. There is no such environment in China. Even if everyone solves the problem, they do not pay attention to accumulation or sharing.
The following is the answer on stackoverflow.com, it is very good, please share it.
http://stackoverflow.com/questions/3401650/stop-mp3-file-from-streaming-in-browsers/3401658#3401658