Javascript 如何在单击图像时打开另存为 .. 对话框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4253299/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:35:37  来源:igfitidea点击:

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

javascriptjqueryhtml

提问by Wasim Shaikh

Here is the code.

这是代码。

On click of the link , the save as dialog should open

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

<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>

How can we achive this using jQuery, or javaScript?

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

采纳答案by Jamie

How about jDownload?

怎么样jDownload

Example usage is shown on the main page.

示例用法显示在主页上。

Edit:The link is now down and there are probably now better plugins for this.

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

Try http://jqueryfiledownload.apphb.com/

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

回答by Hasan

If you are using PHP or any other platform you can always use the force download technique.

如果您使用 PHP 或任何其他平台,您可以随时使用强制下载技术。

This might help:

这可能有帮助:

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

Call the above script as ajax in the click event of image.

在图像的点击事件中将上述脚本调用为ajax。

Cheers

干杯

回答by Pranay Rana

Unfortunately, it works only with IE but not with Firefox.

不幸的是,它只适用于 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>

回答by Mohan Ram

Click to save

点击保存



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


If that doesnt works use this jquery plugins for cross browser function "Cross-browser designMode"

如果这不起作用,请将此 jquery 插件用于跨浏览器功能“Cross-browser designMode”

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

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

回答by mpapis

Try this

尝试这个

html:

html:

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

script:

脚本:

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

most important you need to specify this on server side to send file with disposition attachment when ?downlaod=true was added

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

回答by rbhro

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/>

newer browsers will stick to the old page if a download redirect is made.

如果进行下载重定向,较新的浏览器将停留在旧页面上。