开发者

Html code to download an image

开发者 https://www.devze.com 2023-02-27 06:46 出处:网络
How can I put a link so that when the user clicks on it, the image is downloaded?I have the following, but instead the image is displayed in the browser.

How can I put a link so that when the user clicks on it, the image is downloaded? I have the following, but instead the image is displayed in the browser.

11. Link to image download. <a
 href="http://i1-linux.softpedia-static.com/screenshots/awesome_2.jpg">HERE开发者_开发知识库</a>


The image is displayed due to its content-header, and the way the browser handles the particular file.

Unless you manage the content header for that image, it will be viewed by the browser, not downloaded.

HTML alone will not help you here.


You need to provide content disposition header to overide the default behaviour of showing it in the browser

Content-disposition: attachment; filename=fname.ext

For Example, in PHP:

header('Content-Disposition: attachment; filename="love.gif"');

Source:
http://support.microsoft.com/kb/260519
http://php.net/manual/en/function.header.php


In my experience with IIS 7, changing the MIME type to application/octet-stream only worked in Firefox and did not work in IE9 or Chrome.

Therefore, I would recommend putting "Content-disposition: attachment" in the HTTP Response Headers- it worked on all browsers for me and was very easy to do. Here's how to do it in IIS 7:

1.) Click the folder that contains the files you want users to download (Note that following my steps will force the user to download all files in this folder).
2.) Click HTTP Response Headers while in Features View
3.) Click Add, and type "Content-disposition" under the Name field and "attachment" under the Value field.

This should create a web.config file in the folder you specified in step one like this:

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Content-Disposition" value="attachment" />
        </customHeaders>
    </httpProtocol>
</system.webServer>


You could have php zip the image and link to the zipped file instead of the image. php:Zip


You can do this in windows / IIS.

You change the mime type of the file type you are interested in, into something like application/octet-stream.

Of course, if you did .gif, this would mean that you could not display any .gifs on this website. I believe in IIS you could restrict this condition to a specific folder (or even file).

Go here to read about how to change mime types http://technet.microsoft.com/en-us/library/cc755170(WS.10).aspx

So really it depends on your web server more than anything else, and whether you have access to your web server.


You can instruct your users to right click and save the image from the menu.

An alternative would be to store archives of the images and create a link to that.

0

精彩评论

暂无评论...
验证码 换一张
取 消