图片onclick下载图片javascript / jquery [英] Image onclick download image javascript/jquery

查看:80
本文介绍了图片onclick下载图片javascript / jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想点击下面示例中提供的图片时保存图片。

I want to save the image when you click on the same as provided in below example.

http://www.thrombosisadviser.com/en/resources/image-library/index.php

点击小图片后会出现一个带有下载按钮的弹出窗口,按下下载按钮即可下载图片并准备保存。

After clicking on small image an popup will appear with a download button, by pressing on the download button the image will be downloaded and ready for saving.

我想在javascript / Jquery中做同样的事情。请帮助&建议。

I want to do the same thing in javascript/Jquery only. Please help & suggest.

一如既往地感谢大家。

推荐答案

它无法单独使用javascript下载和保存图像,您还需要使用服务器端脚本。从javascript你只能发起一个请求,它是服务器端脚本,它产生预期的结果。因此,为了下载图像,您可以使用javascript启动ajax请求,并在服务器端脚本中设置http头,如下所示,这将启动浏览器上的下载对话框

It is not possible to download and save the image with javascript alone you also need to use server side scripts too. From javascript you can only initiate a request and it is the server side script which produces the expected result. So in order to download the image you can initiate the ajax request using javascript and in the server side script you have set a http header as shown below which will initiate the download dialog on the browser

内容 - 处置:附件; filename =image.jpg;

Content-Disposition: attachment; filename="image.jpg";

否则你将从服务器获得二进制响应。您还必须使用javascript将其作为文件写入客户端计算机,这是不可能的,因为通过javascript写入文件是违反安全规则的。

Or otherwise you will get it as a binary response from the server. Which again you have to use javascript to write it as a file on the client machine, which is not possible as it is a security violation to write a file via javascript.

希望这可以帮助你

这篇关于图片onclick下载图片javascript / jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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