我希望用户能够下载我服务器上的一些文件,但是当我尝试在互联网上使用这些中的任何一个例子时,似乎对我没什么用。我尝试过这样的代码:
<?php
$size = filesize("Image.png");
header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="Image.png"');
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: ' . $size);
readfile("Image.png");
我甚至试图使用我能找到的最基本的例子,例如:
<?php
header('Content-type: image/png');
header('Content-Disposition: attachment; filename="Image.png"');
readfile('Image.png');
当我测试了这个时,我已经删除了所有其他代码,只使用了这个代码来删除任何由外部源创建的错误。
当我查看控制台时,文件会以正确的标题发送,即
'Content-Disposition: attachment; filename="Image.png"'
但是不显示保存对话框。
我也尝试使用inline而不是内容处理标题中的附件,但这也没有什么区别,我已经在Firefox 8.0.1 Chrome 15.0.874.121和Safari 5.1.1中测试了这个。
答案 0 :(得分:93)
我很确定你没有将mime类型作为JPEG文件下载添加:
header('Content-Type: image/png');
这些标题从未让我失望:
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
$size = filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
答案 1 :(得分:10)
这对我来说非常适合下载PNG和PDF。
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$file_name.'"');
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_url)); //Absolute URL
ob_clean();
flush();
readfile($file_url); //Absolute URL
exit();
答案 2 :(得分:4)
问题是我使用ajax将消息发布到服务器,当我使用直接链接下载文件时一切正常。
我使用了其他Stackoverflow Q&amp; A材料,它对我很有用:
答案 3 :(得分:2)
它为我工作
class MeterDecorator < Draper::Decorator
delegate_all
STATUS_MAPPING = {
uninitialized: '未安装',
good: '良好',
broken: '故障',
disabled: '禁止'
}
def status
STATUS_MAPPING[object.status.to_sym]
end
end
答案 4 :(得分:0)
function PdfView() {
'ngInject';
return {
replace: true,
restrict: "E",
templateUrl: "pdf/directives/pdf-view.html",
link: function(scope, element, attrs) {
},
controller: "PdfController",
controllerAs: 'vm'
};
}
export default {
name: 'pdfView',
fn: PdfView
};
这就是我要做的全部工作。我剥离了所有不需要的东西。
关键是要使用 header('Content-Disposition: attachment; filename=Image.png');
ob_clean();
readfile($file);
答案 5 :(得分:0)
这是我在测试中的一小段代码...显然,通过get传递到脚本可能不是最好的...应该张贴或只是发送一个id并从db抢GUI。我将URL转换为路径。
// Initialize a file URL to the variable
$file = $_GET['url'];
$file = str_replace(Polepluginforrvms_Plugin::$install_url, $DOC_ROOT.'/pole-permitter/', $file );
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
$size = filesize($file);
header( "Content-type: application/octet-stream" );
header( "Content-Disposition: attachment; filename={$quoted}" );
header( "Content-length: " . $size );
header( "Pragma: no-cache" );
header( "Expires: 0" );
readfile( "{$file}" );
答案 6 :(得分:0)
htaccess 解决方案
<filesmatch "\.(?i:doc|odf|pdf|cer|txt)$">
Header set Content-Disposition attachment
</FilesMatch>
您可以阅读此页面: https://www.techmesto.com/force-files-to-download-using-htaccess/