簡體   English   中英

強制瀏覽器使用Javascript window.open下載圖像?

[英]Force Browser to download Image with Javascript window.open?

有沒有辦法讓圖像一旦點擊就下載(沒有右鍵單擊保存圖像 )?

我正在使用一個小的Javascript函數來調用下載頁面:

<a href="#" 
   onclick="window.open('download.php?file=test.jpg', 'download', 'status=0');"
>Click to download</a>

在download.php頁面中我有類似的東西:

$file = $_GET['file'];
header('Content-Description: File Transfer');
header("Content-type: image/jpg");
header("Content-disposition: attachment; filename= ".$file."");
readfile($file);

但它不起作用。 我究竟做錯了什么?
提前致謝!

使用application / octet-stream而不是image / jpg

如果在具有application / octet-stream內容類型的響應中使用[Content-Disposition]標頭,則隱含的建議是用戶代理不應顯示響應,而是直接輸入“保存響應為...”對話。
- RFC 2616 - 19.5.1內容處理

我想你忘了在標題上添加Path

if(isset($_GET['file'])){
    //Please give the Path like this
    $file = 'images/'.$_GET['file'];

    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
}

或者您可以將.htaccess文件用於所有圖像文件。 如果您想強制瀏覽器下載所有圖像(從表格列表中刪除):

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^download$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .(jpe?g|gif|png)$ index.php?file=noFoundFilePage [L,NC]
RewriteCond %{QUERY_STRING} ^download$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .(jpe?g|gif|png)$ - [L,NC,T=application/octet-stream] 

這會查找圖像文件,試圖強制將它們下載到瀏覽器中。 -f RewriteConds還檢查文件是否存在..最后一條規則確保下載僅用於某些文件類型。

這對我有用

header("Pragma: public");
header('Content-disposition: attachment; filename='.$title);
header("Content-type: ".mime_content_type($sample_file));
header('Content-Transfer-Encoding: binary');
ob_clean(); 
flush(); 
readfile($sample_file);

我還在.htaccess中添加了以下內容

SetEnvIf Request_URI "\.jpg$" requested_jpg=jpg
Header add Content-Disposition "attachment" env=requested_jpg

不確定這有幫助嗎?

如果您使用的是Apache服務器,這非常簡單。

  1. 在文件所在的目錄中創建名為.htaccess的文件。 對我來說,這是assets/.htaccess
  2. 將以下內容添加到AddType application/octet-stream .jpg 您可以為所需的每個文件擴展名添加新行。 例如AddType application/octet-stream .pdf
  3. 保存文件
  4. 清除瀏覽器緩存
  5. 刷新您的瀏覽器並享受!

這是一個按鈕,您可以在Internet Explorer中單擊以下載圖片

<html>
<head>
<title>Your title</title>
</head>
<body>
<form><input type="button" value="Click Me" onClick="window.location.href='image.jpg'"></form>
</body>
</html>

添加Content-Disposition: attachment標題后,您應該可以使用普通鏈接:

<a href="download.php?file=test.jpg">Click to download</a>

您嘗試過哪些瀏覽器?

瀏覽器識別jpg URL's並將其交給.htm,所以我認為你不能強迫它在用戶端(雖然不是正面的)。

讀這個

http://apptools.com/phptools/force-download.php

試着改變這個:

header("Content-disposition: attachment; filename= ".$file."");

至:

header("Content-disposition: attachment; filename= ".$file);

如果以上內容對你不起作用,這里有一個強制文件可下載的功能:

    <?php
function downloadFile($file, $type)
{
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: Content-type: $type");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile($file);
}

    downloadFile("sd.jpg", "image/jpg");
    ?>

看看這是否有幫助:

Webkit和Excel文件(PHPexcel)

這是一種強制提示下載某個目錄中的所有文件的方法。

需求

  1. 阿帕奇
  2. mod_headers已啟用

在.htaccess或Apache配置中輸入以下內容。

<Directory /path/to/downloadable/files>
    Header set Content-Disposition attachment
    Header set Content-Type application/octet-stream
</Directory>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM