强制浏览器使用 Javascript window.open 下载图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5648967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force Browser to download Image with Javascript window.open?
提问by Jay Wit
Is there a way to make an image a download once you click on it (without right-click save image as)?
有没有办法在单击后下载图像(无需右键单击将图像另存为)?
I'm using a small Javascript function to call the download page:
我正在使用一个小的 Javascript 函数来调用下载页面:
<a href="#"
onclick="window.open('download.php?file=test.jpg', 'download', 'status=0');"
>Click to download</a>
In the download.php page I have something like:
在 download.php 页面中,我有类似的内容:
$file = $_GET['file'];
header('Content-Description: File Transfer');
header("Content-type: image/jpg");
header("Content-disposition: attachment; filename= ".$file."");
readfile($file);
But it doesn't work. What am I doing wrong?
Thanks in advance!
但它不起作用。我究竟做错了什么?
提前致谢!
采纳答案by Gumbo
Use application/octet-streaminstead of image/jpg:
使用application/octet-stream而不是image/jpg:
If [the Content-Disposition] header is used in a response with the application/octet-stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as...' dialog.
— RFC 2616 – 19.5.1 Content-Disposition
如果在具有 application/octet-stream content-type 的响应中使用 [the Content-Disposition] 标头,则暗示的建议是用户代理不应显示响应,而是直接输入“将响应另存为...”对话。
— RFC 2616 – 19.5.1 内容处置
回答by Kaartikeyan R
I think you forgot to add Path on the header
我想你忘了在标题上添加路径
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;
}
}
回答by Bery
Or you can use .htaccess file for all your image files. In case you want to force the browser to download all your images (f.e. from a table list):
或者您可以将 .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]
This looks for image files a tries to force download them into the browser. The -f RewriteConds also checks that the file exsist.. The last rule ensures that download is used only for certain file types.
这会寻找图像文件,试图强制将它们下载到浏览器中。-f RewriteConds 还检查文件是否存在。最后一条规则确保下载仅用于某些文件类型。
回答by Michael Stott
This worked for me
这对我有用
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);
I also added the following to .htaccess
我还将以下内容添加到 .htaccess
SetEnvIf Request_URI "\.jpg$" requested_jpg=jpg
Header add Content-Disposition "attachment" env=requested_jpg
Not sure if that helps?
不确定这是否有帮助?
回答by kdev
If you're on an apache server this is really simple.
如果您在 apache 服务器上,这非常简单。
- Create a file called .htaccess in the directory in which your files exist. For me it was
assets/.htaccess
. - Add the following to the file
AddType application/octet-stream .jpg
. You can add a new line for each file extension that you need. E.g.AddType application/octet-stream .pdf
- Save your file
- Clear your browser cache
- Refresh your browser and enjoy!
- 在您的文件所在的目录中创建一个名为 .htaccess 的文件。对我来说是
assets/.htaccess
。 - 将以下内容添加到文件中
AddType application/octet-stream .jpg
。您可以为您需要的每个文件扩展名添加一个新行。例如AddType application/octet-stream .pdf
- 保存您的文件
- 清除浏览器缓存
- 刷新您的浏览器并享受!
回答by anonomus
This is a button you can click on in internet explorer to download pic
这是一个按钮,您可以在 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>
回答by Martijn
Once you've added the Content-Disposition: attachment
header, you should be able to use a normal link:
添加Content-Disposition: attachment
标题后,您应该可以使用普通链接:
<a href="download.php?file=test.jpg">Click to download</a>
What browsers have you tried this with?
你用什么浏览器试过这个?
回答by SIFE
Try to change this:
尝试改变这一点:
header("Content-disposition: attachment; filename= ".$file."");
To:
到:
header("Content-disposition: attachment; filename= ".$file);
If the above doesn't work to you, here a function to force a file to be downloadable:
如果以上对您不起作用,这里有一个强制文件可下载的功能:
<?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");
?>
回答by diEcho
Browsers recognize jpg URL's
and hand them over just like a .htm, so I don't think you can force it on the user-end (not positive on that, though).
浏览器识别 jpgURL's
并像 .htm 一样将它们传递出去,因此我认为您不能在用户端强制使用它(尽管对此并不乐观)。
Read this
读这个
回答by Richard Ayotte
Here's a way to force all files in a certain directory to be prompted for a download.
这是一种强制提示下载某个目录中的所有文件的方法。
Requirement
要求
- Apache
- mod_headersenabled
- 阿帕奇
- 启用mod_headers
In your .htaccess or Apache configuration put the following.
在您的 .htaccess 或 Apache 配置中输入以下内容。
<Directory /path/to/downloadable/files>
Header set Content-Disposition attachment
Header set Content-Type application/octet-stream
</Directory>