href图像链接下载点击

我在Web应用程序中生成正常的链接,如<a href="/path/to/image"><img src="/path/to/image" /></a>

当我点击链接时,它会在新页面中显示图片。 如果你想保存图片,那么你需要右键单击它并select“另存为”

我不希望这种行为,我想有一个下载框popup,当我点击链接,是可能的只是与HTML或JavaScript? 怎么样?

如果没有,我想我将不得不写一个download.php脚本,并将其作为参数的文件名称调用到HREF?

 <a download="custom-filename.jpg" href="/path/to/image" title="ImageName"> <img alt="ImageName" src="/path/to/image"> </a> 

它还没有完全支持http://caniuse.com/#feat=download ,但你可以使用modernizr https://modernizr.com/download/?adownload-setclasses在非核心检测下 )来检查支持浏览器。

为图像或html创build下载链接最简单的方法是设置下载属性 ,但是这种解决scheme只适用于现代浏览器。

 <a href="/path/to/image" download="myimage"><img src="/path/to/image" /></a> 

“myimage”是要下载的文件的名称。 扩展名将自动添加在这里例子

 <a href="download.php?file=path/<?=$row['file_name']?>">Download</a> 

的download.php:

 <?php $file = $_GET['file']; download_file($file); function download_file( $fullPath ){ // Must be fresh start if( headers_sent() ) die('Headers Sent'); // Required for some browsers if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); // File Exists? if( file_exists($fullPath) ){ // Parse Info / Get Extension $fsize = filesize($fullPath); $path_parts = pathinfo($fullPath); $ext = strtolower($path_parts["extension"]); // Determine Content Type switch ($ext) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpeg": case "jpg": $ctype="image/jpg"; break; default: $ctype="application/force-download"; } header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-Type: $ctype"); header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".$fsize); ob_clean(); flush(); readfile( $fullPath ); } else die('File Not Found'); } ?> 

不,不是的。 您需要在服务器上发送一个Content-Disposition标头,将该文件设置为附件,而不是内联。 你可以用普通的Apacheconfiguration来做到这一点。

我find了一个使用mod_rewrite的例子 ,虽然我知道有一个更简单的方法。

尝试这个…

 <a href="/path/to/image" download> <img src="/path/to/image" /> </a> 

如果您使用HTML5,则可以将属性“下载”添加到链接。

 <a href="/test.pdf" download> 

http://www.w3schools.com/tags/att_a_download.asp

你不能用纯html / javascript来做。 这是因为你有一个单独的连接到networking服务器来检索一个单独的文件(图像)和一个普通的networking服务器将提供文件的内容头设置,以便浏览器读取内容types将决定该types可以在内部处理。

强制浏览器不在内部处理文件的方法是更改​​标题(content-disposition优先或内容types),以便浏览器不会尝试在内部处理文件。 您可以通过在dynamic设置标头的networking服务器(例如download.php)上编写脚本,或者通过将networking服务器configuration为为要下载的文件返回不同的标头来实现。 你可以在网页服务器的每个目录下做到这一点,这将允许你离开而不写任何PHP或JavaScript – 只需将所有的下载图像在这一个位置。

简单的代码图像下载与图像点击使用PHP

 <html> <head> <title> Download-Button </title> </head> <body> <p> Click the image ! You can download! </p> <?php $image = basename("http://localhost/sc/img/logo.png"); // you can here put the image path dynamically //echo $image; ?> <a download="<?php echo $image; ?>" href="http://localhost/sc/img/logo.png" title="Logo title"> <img alt="logo" src="http://localhost/sc/img/logo.png"> </a> </body> 

图像下载与使用图像点击!

我做了这个简单的代码!:)

 <html> <head> <title> Download-Button </title> </head> <body> <p> Click the image ! You can download! </p> <a download="logo.png" href="http://localhost/folder/img/logo.png" title="Logo title"> <img alt="logo" src="http://localhost/folder/img/logo.png"> </a> </body> </html> 

有简单的两种方法来避免打开任何文件,而不是下载。 1.)一个简单的解决scheme,就是把图片的扩展名改成浏览器不能理解的东西,比如myimage.zzz,但是下载后需要告诉访问者改变扩展名。 也许不那么实际。 2.)把图像放在一个zip文件中,我通常用不想立即打开的文件来做这件事。

下载标签不适用于Safari或IExplorer。 我希望有帮助?