[英]Initiate image download using jQuery
I want my users to be able to download images from my website using jQuery. 我希望我的用户能够使用jQuery从我的网站下载图像。 For example, I have an image like this below on my website.
例如,我在我的网站上有如下图像。 When user clicks "Download" button that image must be downloaded to user system.
当用户单击“下载”按钮时,该图像必须下载到用户系统。 What can I do to achieve this?
我能做些什么来实现这个目标?
<div class="download"><img src="http://m.com/hello.png"><div>
EDIT : PHP code is also welcome, for example download.php?url=http://m.com/hello.png
, 编辑:也欢迎PHP代码,例如
download.php?url=http://m.com/hello.png
,
PS: http://m.com/hello.png is an external URL. PS: http : //m.com/hello.png是一个外部URL。
For server side, you could direct the image click to a PHP page that would force the download. 对于服务器端,您可以将图像单击指向强制下载的PHP页面。
On your HTML page, you would have: 在您的HTML页面上,您将拥有:
If PNG file 如果是PNG文件
<a href="download.php?ext=png"><img src="hello.png" /></a>
or if JPG file 或者如果是JPG文件
<a href="download.php?ext=jpg"><img src="hello.jpg" /></a>
Then a PHP page download.php
as shown below 然后是PHP页面
download.php
,如下图所示
<?php
if($_GET['ext'] == 'png') {
$ext = 'png';
} elseif($_GET['ext'] == 'jpg') {
$ext = 'jpg';
}
header('Content-disposition: attachment; filename=Name-of-Image.'.$ext);
header('Content-type: image/'.$ext);
readfile('http://m.com/hello.'.$ext);
?>
PHP Code: PHP代码:
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="filename.png"');
readfile('hello.png');
You will not be able to do it reliably without using PHP. 如果不使用PHP,您将无法可靠地完成它。 I found a previous question on this website that can assist you with this: Force Download an Image Using Javascript
我在本网站上发现了一个可以帮助您解决此问题的问题: 使用Javascript强制下载图像
Actually there is a method that relies on HTML5 in there, but I'm not quite sure you could consider that reliable yet. 实际上有一种方法依赖于HTML5,但我不太确定你能否认为它是可靠的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.