The Web site contains more than just web pages, and sometimes you need to provide files that users can download. Putting your files on the server and attaching links to the pages is just the first step, and you also need to be aware that the HTTP response headers affect file downloads.
Tip One: Force the download and control the file name
Providing a download link in HTML is simple:
<
a
href
=
"http://download.httpwatch.com/httpwatch.exe"
>Download<
a
>
This works well for binary files that browsers do not know how to render, such as setup and zip files. A dialog box appears that allows the user to store the file locally.
The problem is that if the file renders itself, different browsers behave differently. For example, if you link a plain text file, the browser will open it and will not be prompted to save the download.
You can join the following response header file to force the File Download dialog box to be used.
Content-disposition:attachment; Filename=<file name.ext>
The head can also control the default file name, which can help you easily generate content like getfile.aspx, but you want to provide a more meaningful filename to the user.
For static content, you can manually configure additional header files on your Web server. For example, the following is set up in IIS:
For dynamically generated content, you need to add this header to the server-side code of the Web page.
After adding the head, the browser always prompts the user to download the file: