Html 点击下载图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1227157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download a picture OnClick
提问by
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.
当您点击图片时,您实际上是如何下载图片的?是否有某种 javascript 代码可以做到这一点?这是我如何用纯 HTML 显示图像。
<a href="#"><img src="myPic.png" border="0"></a>
回答by Quentin
Assuming by "download" you mean "Cause the user's browser to throw up the 'save or open' dialogue" — you can't.
假设“下载”的意思是“使用户的浏览器弹出“保存或打开”对话框”——你不能。
You could link to another URL offering the same file but with the Content-Disposition header set to attachment. The specifics of how you would provide such a resource at that URL would depend on the server side capabilities on offer to you.
您可以链接到提供相同文件的另一个 URL,但将 Content-Disposition 标头设置为 attach。您将如何在该 URL 上提供此类资源的具体细节取决于提供给您的服务器端功能。
回答by DisgruntledGoat
Most people right-click on the image and choose "Save image as..."
大多数人右键单击图像并选择“将图像另存为...”
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:
替代方法是链接以使用设置“Content-type”和“Content-disposition”标头的服务器端脚本。在 PHP 中,这将类似于文档中的这个示例:
header('Content-Type: image/png'); // or 'image/jpg' or 'image/gif'
header('Content-Disposition: attachment; filename="filename.png"');
readfile('original.png');
UPDATE: Since you say the image is generated by a PHP script in the first place, there are a few options:
更新:既然你说图像首先是由 PHP 脚本生成的,那么有几个选项:
- 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.
- 将 URL (sig.php?...) 作为参数放入
readfile
. 这意味着任何点击下载的人都会受到双重处理。 - 将图像生成脚本的输出缓存到文件系统,然后将该文件传递给
readfile
. - 编辑图像生成脚本以接受一个额外的参数,例如
mode=download
您将要输出图像的位置,如果该参数存在,请设置上面的两个标题。
回答by Greg Jones
I trick this out a bit - I zip the picture and put it somewhere. Then, instead of using a fancy script or server-side stuff, I make the picture's link the location of the .zip file. The user get's warned/asked if they want to download this zip and voila...
我有点把它搞砸了——我把图片拉上拉链放在某个地方。然后,我没有使用花哨的脚本或服务器端的东西,而是将图片的链接设为 .zip 文件的位置。用户收到警告/询问他们是否要下载此 zip 并瞧...
But this is usually in an area where the user is someone who would want the image...
但这通常是在用户是想要图像的人的区域中......
回答by Marius
Do you want to open the picture in a new window/tab? Or do you want to download it to the users computer? If you want the user to save the image, then you need to set the content-type of the file they receive:
您想在新窗口/标签中打开图片吗?或者您想将其下载到用户计算机上?如果您希望用户保存图像,则需要设置他们收到的文件的内容类型:
<?php
$file = $_GET['file'];
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
?>
Remember to check the input so people can't download source files.
请记住检查输入,以便人们无法下载源文件。