4 ways to force download jpg or txt files

Source: Internet
Author: User
Tags header browser cache

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.

This kind of situation is often encountered on the Internet, when downloading JPG or txt files, directly plus the file link, click and do not jump out of the choice to save pop-up box, but in the Web browser directly display, to solve the problem is actually very simple, that is, through the HTTP header file to set up, This article has introduced 4 kinds of methods altogether, as long as the reasonable use can control the webpage file downloading way freely.

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:

Tip 2: Use a valid HTTP cache

As with any other content, it is worth setting the HTTP cache to maximize download speed and reduce the cost of bandwidth. Regular content needs to expire immediately or be cached forever.

Our example of downloading HTTP specification (RFC2616) can always be cached because it does not want to change it. HttpWatch can see these, we set a very long expiration time and set the Cache-control value to "public".

This will allow future downloads of files to be transmitted either from the local browser cache or from the intermediary agent. If the file changes frequently, you may need it to expire immediately so that you always download a new copy. You can set the expires to-1 or any previous date.

Tip 3: Do not break ie browser HTTPS download

With Cache-control response headers, it is easy to use no storage and no caching to prevent any one file cache from being updated frequently.

Cache-Control: no-store, no-cache

This works in Firefox, but you have to be careful with Internet Explorer. It interprets these tags as--when you are using HTTPS, the content will never be stored to disk, causing the File Download dialog box to hang for several minutes at 0%.

It will eventually display an error message:

In the post on Eric Lawrence's Ieinternals Blog This article, there are more information on this issue and other reasons.

Hint #4: Don't forget the setup Analytics

On your site, you may want to follow along with downloading files and other metrics. A JavaScript based solution, such as Google Analytics, is popular, but does not show file downloads by default. This is because downloading a file does not cause any JavaScript to execute. Using Google Analytics, you need to add a Onlick handler to track downloads.

< a onclick="pageTracker._trackPageview("/httpwatch.exe");"href="...">Download</a>

You can see the file started downloading before Google Analytics was invoked.

Reprint Address: http://www.denisdeng.com/?p=773

Original address: Four Tips for Setting up HTTP File Downloads

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.