强制浏览器使用jquery点击链接下载图像 [英] Force browser to download an image on click of a link using jquery

查看:121
本文介绍了强制浏览器使用jquery点击链接下载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过点击链接开始下载图片。

I would like to start download of an image on click of a link.

<a id="downloadImage" href="imagepath">Click here to download</a>

我知道我们可以使用HTML5的下载属性(强制浏览器在点击链接后保存文件),但是我不想使用它因为它不会在旧版本的浏览器中工作。
我尝试过这个方法:
使用Javascript / jQuery下载文件
,但它会打开iframe中的图像。

I know we can use download attribute of HTML5 (Force a browser to save file as after clicking link) but I would not like to use it as it will not be working in older versions of browsers. I did tried the method here: Download File Using Javascript/jQuery but it opens the image in the iframe .

有人可以帮我强制浏览器使用jquery下载链接的图片吗?

Can anybody help me to force a browser to download an image onclick of a link using jquery?

推荐答案

据我所知,这个问题没有客户端跨浏览器解决方案(使用jQuery无关紧要或任何其他UI工具包)。为了触发浏览器下载文件,您需要做的是向服务器响应添加一些HTTP标头:

As far as I know there is no client-side cross-browser solution for this issue (doesn't matter using jQuery or any other UI toolkit). What you need to do in order to trigger browser to download a file is to add some HTTP headers to the server response:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename=image.jpg

这个信息也可能对您有所帮助。

This post may also be helpful for you.

这篇关于强制浏览器使用jquery点击链接下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆