使用jQuery启动图像下载 [英] Initiate image download using jQuery

查看:57
本文介绍了使用jQuery启动图像下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的用户能够使用jQuery从我的网站下载图像.例如,我的网站下面有一张这样的图片.当用户单击下载"按钮时,该图像必须下载到用户系统.我该怎么做?

I want my users to be able to download images from my website using 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>

也欢迎使用PHP代码,例如download.php?url=http://m.com/hello.png

EDIT : PHP code is also welcome, for example download.php?url=http://m.com/hello.png,

PS: http://m.com/hello.png 是一个外部URL.

PS: http://m.com/hello.png is an external URL.

推荐答案

对于服务器端,您可以将图像单击直接定向到PHP页面,以强制下载.

For server side, you could direct the image click to a PHP page that would force the download.

在HTML页面上,您将:

On your HTML page, you would have:

如果是PNG文件

<a href="download.php?ext=png"><img src="hello.png" /></a>

或JPG文件

<a href="download.php?ext=jpg"><img src="hello.jpg" /></a>

然后显示一个PHP页面download.php,如下所示

Then a PHP page download.php as shown below

<?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);

?>

这篇关于使用jQuery启动图像下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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