[英]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服务器,这非常简单。
assets/.htaccess
。 AddType application/octet-stream .jpg
。 您可以为所需的每个文件扩展名添加新行。 例如AddType application/octet-stream .pdf
这是一个按钮,您可以在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,所以我认为你不能强迫它在用户端(虽然不是正面的)。
读这个
试着改变这个:
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");
?>
看看这是否有帮助:
这是一种强制提示下载某个目录中的所有文件的方法。
需求
在.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.