Link to download image instead of view image

人走茶凉 提交于 2020-01-01 03:53:24

问题


I need to provide website visitors a link to download an original high resolution image. If I simply link to the image with a href="image.jpg", the browser displays the image. How do I make the browser download it?

I'm using amazon S3 to server these images... If this requires having a special header set on the image, I'm sure it's possible.. but I also want to display these images sometimes. Any tips on making this work with S3 would be great.


回答1:


You are right. There are some headers needed to do this. These would be:

Content-type: image/png
Content-Disposition: attachment; filename="some_image.png"

I am not sure how S3 handles this but if you want do display these images, the Content-Disposition header should be left out.

EDIT: Displaying the image via <img src="some_image.png" alt="" /> continues to work even with both headers being sent. The image displays nicely in my tests.




回答2:


You can use the content-disposition header to let the client download the image:

Content-Disposition: attachment; filename=<file name.ext>


来源:https://stackoverflow.com/questions/3651093/link-to-download-image-instead-of-view-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!