2011-05-20 36 views
0

我有一個請求,以PDF文件的相同方式下載JPG圖像。如何製作像PDF文件一樣的jpg圖像下載呢?

目前,如果我將jpg圖像作爲鏈接添加到網頁中,它將在另一個瀏覽器窗口中打開,而不是實際下載到用戶計算機。但是,一個PDF文件將。

下面是標準代碼:

<a href="/images/my-image.jpg">download image</a> 

我希望有這方面的指導。

謝謝。

+1

您將需要修改您的網絡服務器配置或編寫一個腳本當文件發送到瀏覽器時修改標題。瀏覽器將一直嘗試顯示圖像。 – 2011-05-20 14:20:01

+2

您是否使用任何服務器端語言?如果是這樣,我可以想到的一種方式是使用內容處置標題。 http://stackoverflow.com/questions/1012437/uses-of-content-disposition-in-an-http-response-header – DeaconDesperado 2011-05-20 14:20:08

回答

2

如果你在服務器上有PHP,你可以這樣做。然後

<?php 
// Force download of image file specified in URL query string and which 
// is in the same directory as this script: 
if(!empty($_GET['img'])) 
{ 
    $filename = basename($_GET['img']); // don't accept other directories 
    $size = @getimagesize($filename); 
    $fp = @fopen($filename, "rb"); 
    if ($size && $fp) 
    { 
     header("Content-type: {$size['mime']}"); 
     header("Content-Length: " . filesize($filename)); 
     header("Content-Disposition: attachment; filename=$filename"); 
     header('Content-Transfer-Encoding: binary'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     fpassthru($fp); 
     exit; 
    } 
} 
header("HTTP/1.0 404 Not Found"); 
?> 

HTML看起來像這樣

<img src="/images/download.php?img=imagename.jpg" alt="test"> 
1

PDF內容類型如何從您的瀏覽器打開取決於許多不同的設置。它可能會受到瀏覽器設置中程序的內容類型關聯的影響。它也可能受到PDF閱讀器本身設置的影響,例如,Adobe Reader在下編輯,首選項...,Internet選項卡有一個選項,用於在瀏覽器或應用程序中打開PDF文檔直。

3

要做到這一點是設置你的web服務器,或者服務器端腳本(PHP,asp.net等)服務的最佳方式與the Content-Disposition header您的JPG文件:

Content-Disposition: attachment; filename="my-image.jpg" 

腳本:

服務器:

2

您可以使用建議這裏提到: http://answers.yahoo.com/question/index?qid=20080417163416AAjlb7T

這將是這個樣子:

access_file.php: 

<?php 

$f= $_GET['file']; 

$filename = explode("/", $f); 
$filename = $filename[(count($filename)-1)]; 

header('Content-Disposition: attachment; filename="'.$filename.'"'); 
readfile($f); 

?> 

如果你堅持你的圖像access_file.php文件夾,然後你可以只使用link:

<a href="/images/access_file.php?file=my-image.jpg" /> 
0

在HTML 5,我們有一個屬性稱爲下載

<a href="img/myimage.jpg" download="myimage.jpg"><img src="img/myimage.jpg" /></a> 
相關問題