【问题标题】:Force Browser to download Image with Javascript window.open?强制浏览器使用 Javascript window.open 下载图像?
【发布时间】:2011-08-04 16:09:00
【问题描述】:

有没有一种方法可以在您单击图像后将其下载(无需右键单击将图像另存为)?

我正在使用一个小的 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);

但它不起作用。我做错了什么?
提前致谢!

【问题讨论】:

  • 请注意,使用此代码,任何人都可以从您的服务器下载任何可能的文件:只需在 URL 中指定任何文件的路径,瞧,您会得到它的内容...
  • @Pascal;别担心,它是安全的,这只是代码的一小部分
  • 您的 download.php 中可能存在 PHP 错误,但在上面的代码中是正确的并且可以正常工作。我已经测试过:) 你可以通过在 header 之前写 exit 来测试。

标签: php javascript


【解决方案1】:

使用application/octet-stream 代替image/jpg

如果在内容类型为 application/octet-stream 的响应中使用了 [the Content-Disposition] 标头,则暗示用户代理不应显示响应,而是直接输入“将响应另存为”。 ..' 对话框。
RFC 2616 – 19.5.1 Content-Disposition

【讨论】:

  • 保存为jpg格式,文件名正确。但是当我打开文件时它已损坏?所以看起来它实际上不是 jpeg
  • @Jay Wit:您发送的数据可能不仅仅是文件内容(例如一些空格等)。
  • @Gumbo;有些文件名/目录包含空格是的,但这是个问题吗?
  • @Jay Wit:打印文件内容的是readfile。只需使用十六进制编辑器即可查看已发送的实际数据。
  • @Jay Wit:除了readfile 之外不要做任何输出或缓冲它,这样它就不会被发送到客户端。
【解决方案2】:

我想你忘了在标题上添加路径

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

【讨论】:

    【解决方案3】:

    或者您可以为所有图像文件使用 .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 还检查文件是否存在。最后一条规则确保下载仅用于某些文件类型。

    【讨论】:

    • 谢谢 :-) 我也最喜欢它 ;-) 它很干净,不使用 PHP 作为流。仅适用于所选目录。唉,它并不适合所有用例..
    • 为了让它在 IE 9 中适用于 JPG,我还必须将 Header set Content-Disposition attachment 添加到 Apache 配置中。
    【解决方案4】:

    这对我有用

    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
    

    不确定这是否有帮助?

    【讨论】:

      【解决方案5】:

      如果您使用的是 apache 服务器,这真的很简单。

      1. 在您的文件所在的目录中创建一个名为 .htaccess 的文件。对我来说是assets/.htaccess
      2. 将以下内容添加到文件AddType application/octet-stream .jpg。您可以为所需的每个文件扩展名添加一个新行。例如。 AddType application/octet-stream .pdf
      3. 保存您的文件
      4. 清除浏览器缓存
      5. 刷新您的浏览器,尽情享受吧!

      【讨论】:

        【解决方案6】:

        添加 Content-Disposition: attachment 标头后,您应该可以使用普通链接了:

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

        你用什么浏览器试过这个?

        【讨论】:

        • 它一直在下载页面本身,我做错了什么吗?我已经在 Chrome 和 Firefox 中尝试过。
        • @JayWit:它会下载 PHP 代码吗?如果是这样,您的 Web 服务器被配置为在该目录中运行 PHP 脚本。你用的是什么服务器?如果它不下载 PHP 代码,那么它会下载什么
        • 它下载一个损坏的 jpeg 文件,我认为是一个只有 jpg 文件类型的空文件。不过不确定。有什么想法吗?
        • 使用 Notepad.exe 打开下载的 .jpg 文件;那你看到了什么?
        【解决方案7】:

        这是一个您可以在 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>
        

        【讨论】:

          【解决方案8】:

          浏览器会识别 jpg URL's 并将它们像 .htm 一样移交,因此我认为您不能在用户端强制使用它(不过,对此并不积极)。

          阅读本文

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

          【讨论】:

          • 他们识别标题。如果没有预设,他们会查看扩展名。
          【解决方案9】:

          看看这是否有帮助:

          Webkit and Excel file(PHPexcel)

          【讨论】:

          • 这对我来说效果很好。你是如何使用它的?我逐字使用了您的下载链接、名为 test.jpg 的 jpg 和我的函数。 pastebin.com/xPkS9NhU
          • 旁注:jpg 与 download.php 脚本位于同一文件路径中。
          【解决方案10】:

          尝试改变这一点:

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

          【讨论】:

          • 我更新了我的代码,它现在对我有用,只要确保你指向现有文件。
          • 仍然无法正常工作,我的 download.php 文件旁边有一个图像 sd.jpg,它仍然是损坏的 jpeg。这个问题可能是因为我在我的本地主机上吗?
          • 我也在本地,尝试清除浏览器缓存,也尝试另一个图像。
          【解决方案11】:

          这是一种强制要求下载某个目录中的所有文件的方法。

          要求

          1. 阿帕奇
          2. mod_headers 已启用

          在您的 .htaccess 或 Apache 配置中放入以下内容。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-10-11
            • 2013-11-08
            • 1970-01-01
            • 2012-05-15
            • 2013-07-05
            • 1970-01-01
            相关资源
            最近更新 更多