我有一个pdf文件,位于我的网页根目录下.我想../cvs使用php向我的用户提供文件.
这是我的代码:
header('Content-type: application/pdf');
$file = file_get_contents('/home/eamorr/sites/eios.com/www/cvs/'.$cv);
echo $file;
Run Code Online (Sandbox Code Playgroud)
但是当我打电话给这个php页面时,没有任何内容被打印 我想简单地提供存储名称所在的pdf文件$cv(例如$cv = 'xyz.pdf').
对这个PHP页面的ajax响应返回pdf(gobbldy-gook!)的文本,但是我想要文件,而不是gobbldy-gook!
我希望这是有道理的.
提前谢谢了,
这是我正在使用的AJAX
$('#getCurrentCV').click(function(){
var params={
type: "POST",
url: "./ajax/getCV.php",
data: "",
success: function(msg){
//msg is gobbldy-gook!
},
error: function(){
}
};
var result=$.ajax(params).responseText;
});
Run Code Online (Sandbox Code Playgroud)
我希望提示用户下载文件.
tim*_*dev 11
不要使用XHR(Ajax),只需链接到下面的脚本即可.脚本输出的HTTP标头将指示浏览器下载文件,因此用户不会离开当前页面.
<?php
// "sendfile.php"
//remove after testing - in particular, I'm concerned that our file is too large, and there's a memory_limit error happening that you're not seeing messages about.
error_reporting(E_ALL);
ini_set('display_errors',1);
$file = '/home/eamorr/sites/eios.com/www/cvs/'.$cv;
//check sanity and give meaning error messages
// (also, handle errors more gracefully here, you don't want to emit details about your
// filesystem in production code)
if (! file_exists($file)) die("$file does not exist!");
if (! is_readable($file)) die("$file is unreadable!");
//dump the file
header('Cache-Control: public');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="some-file.pdf"');
header('Content-Length: '.filesize($file));
readfile($file);
?>
Run Code Online (Sandbox Code Playgroud)
然后,简化您的javascript:
$('#getCurrentCV').click(function(){
document.location.href="sendfile.php";
});
Run Code Online (Sandbox Code Playgroud)