2010-11-23 27 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。

乾杯

相關問題