2010-11-23 28 views
4

这是代码。如何在点击图像时打开另存为..dialog?

链接的点击,另存为对话框应该打开

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

+7

这不是在客户端完成的。它在服务器端通过目标URL上的HTTP标头完成。 – Asaph 2010-11-23 06:23:03

+0

PHP,例如:http://stackoverflow.com/questions/3718962/force-file-download-in-php或http://stackoverflow.com/questions/3930490/javascript-handling-of-php-readfile- octet-stream – Ben 2010-11-23 06:25:33

回答

0

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

新的浏览器将坚持旧的页面如果下载重定向而成。

0

Click to save


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

如果这并不工程使用的跨浏览器功能 “跨浏览器的designMode” 这个jQuery插件

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

-1

试试这个

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"/>'); 
}); 

最重要的,你需要在服务器端指定此发送文件,配置附件时立即下载=真加入

+0

这将不会触发SaveAs对话框,而`_new`只是错误的。 – Quentin 2010-12-13 09:23:00

1

如果您使用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。

干杯

相关问题