繁体   English   中英

如何点击图片打开另存为..dialog?

[英]How to open the Save as ..dialog on click of the image?

这是代码。

单击该链接后,应该打开另存为对话框

<a href="http://www.experts-exchange.com/xp/images/newNavLogo.png" target="_new">
<img src="http://www.experts-exchange.com/xp/images/newNavLogo.png" align="left" alt="" />
</a>

我们如何使用jQuery或javaScript实现这一目标?

如果您使用的是PHP或任何其他平台,则可以始终使用强制下载技术。

这可能有所帮助:

<?php 
$file = 'tag_cloud.gif';
if(!file){
  // File doesn't exist, output error
  die('file not found');
}else{
  // Set headers
  header("Cache-Control: public");
  header("Content-Description: File Transfer");
  header("Content-Disposition: attachment; filename=$file");
  header("Content-Type: image/gif");
  header("Content-Transfer-Encoding: binary");
  // Read the file from disk
  readfile($file);
}
?>

在图像的单击事件中将上述脚本称为ajax。

干杯

jDownload怎么样?

示例用法显示在主页面上。

编辑:链接现在已关闭,现在可能有更好的插件。

试试http://jqueryfiledownload.apphb.com/

不幸的是,它只适用于IE,但不适用于Firefox。

</head>
<script>

 function saveImageAs (imgOrURL) {
    if (typeof imgOrURL == 'object')
      imgOrURL = imgOrURL.src;
    window.win = open (imgOrURL);
    setTimeout('win.document.execCommand("SaveAs")', 500);
  }
</script>
<body>

  <A HREF="javascript: void 0"
     ONCLICK="saveImageAs(document.anImage); return false"
  >save image</A>
  <IMG NAME="anImage" SRC="../apache_pb2.gif">
</body>

点击保存


$("#img").click(function() {
 document.execCommand('SaveAs','1','give img location here');
});

如果这不起作用使用此jquery插件进行跨浏览器功能“跨浏览器designMode”

http://plugins.jquery.com/project/designMode

function dl(obj){
   window.location = obj.src;
}

<br/>
..........

<br/>

<img src="http://www.experts-exchange.com/xp/images/newNavLogo.png" align="left" alt="" onClick="dl(this);"/>
<br/>

如果进行下载重定向,较新的浏览器将粘贴到旧页面。

试试这个

HTML:

<img src="http://www.experts-exchange.com/xp/images/newNavLogo.png" align="left" alt="" />

脚本:

$('img').each(function(){
  $(this).wrap('<a href="'+this.src+'?download=true" target="_blank"/>');
});

最重要的是,当添加了?downlaod = true时,您需要在服务器端指定此项以发送带有处置附件的文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM