强制浏览器使用jquery点击链接下载图像

编程入门 行业动态 更新时间:2024-10-25 22:28:29
本文介绍了强制浏览器使用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点击链接下载图像

本文发布于:2023-11-01 20:36:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550348.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   浏览器   链接   jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!