[英]Force Browser to download Image with Javascript window.open?
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! 提前致谢!
Use application/octet-stream instead 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.
如果在具有application / octet-stream内容类型的响应中使用[Content-Disposition]标头,则隐含的建议是用户代理不应显示响应,而是直接输入“保存响应为...”对话。
— RFC 2616 – 19.5.1 Content-Disposition- RFC 2616 - 19.5.1内容处理
I think you forgot to add Path on the header 我想你忘了在标题上添加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;
}
}
Or you can use .htaccess file for all your image files. 或者您可以将.htaccess文件用于所有图像文件。 In case you want to force the browser to download all your images (fe from a table list):
如果您想强制浏览器下载所有图像(从表格列表中删除):
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还检查文件是否存在..最后一条规则确保下载仅用于某些文件类型。
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? 不确定这有帮助吗?
If you're on an apache server this is really simple. 如果您使用的是Apache服务器,这非常简单。
assets/.htaccess
. assets/.htaccess
。 AddType application/octet-stream .jpg
. AddType application/octet-stream .jpg
。 You can add a new line for each file extension that you need. AddType application/octet-stream .pdf
AddType application/octet-stream .pdf
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>
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? 您尝试过哪些浏览器?
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). 浏览器识别jpg
URL's
并将其交给.htm,所以我认为你不能强迫它在用户端(虽然不是正面的)。
Read this 读这个
http://apptools.com/phptools/force-download.php http://apptools.com/phptools/force-download.php
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");
?>
See if this helps: 看看这是否有帮助:
Here's a way to force all files in a certain directory to be prompted for a download. 这是一种强制提示下载某个目录中的所有文件的方法。
Requirement 需求
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.