下载图片OnClick [英] Download a picture OnClick
本文介绍了下载图片OnClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当您点击图片时,如何实际下载图片?有没有一些javascript代码呢?这是我用纯HTML显示图像的方式。
How do you actually download a picture when you click on it? Is there some kind of a javascript code to do just that? Here is how i show the image with pure HTML.
<a href="#"><img src="myPic.png" border="0"></a>
推荐答案
大多数人右键点击图片,选择将图像保存为...
Most people right-click on the image and choose "Save image as..."
替代方法是链接到使用设置Content-type和Content-disposition头的服务器端脚本。在PHP中,这个例子来自文档:
The alternate is to link to use a server-side script that sets a "Content-type" and "Content-disposition" header. In PHP, that would be something like this example from the docs:
header('Content-Type: image/png'); // or 'image/jpg' or 'image/gif'
header('Content-Disposition: attachment; filename="filename.png"');
readfile('original.png');
更新:由于您说的图像是由PHP脚本生成的,首先,几个选项:
UPDATE: Since you say the image is generated by a PHP script in the first place, there are a few options:
- 将URL(sig.php?...)作为参数添加到
readfile
。这将意味着任何点击下载的人都可以进行双重处理。
- 将图像生成脚本的输出缓存到文件系统,然后将该文件传递到
readfile
。 - 编辑图像生成脚本以接受额外的参数,如
mode = download
,然后您即将输出图像,如果参数存在,设置上述两个标题。
- Put the URL (sig.php?...) as the parameter to
readfile
. This will mean double processing for anyone who clicks to download. - Cache the output from your image generation script to the filesystem, then pass that file to
readfile
. - Edit the image generation script to accept an extra parameter like
mode=download
and then where you are about to output the image, if the parameter is present, set those two headers above.
这篇关于下载图片OnClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文