我的服务器上有一个生成的xls文件,我想将其推送到客户端进行下载,但似乎无法使其正常工作。以下是我到目前为止的情况:
$xlsFile = 'test.xls';
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$xlsFile");
header("Content-Transfer-Encoding: binary ");
exit();
我的excel文件是正确的,如果我从服务器打开它可以打开但是如何将此文件推送到客户端进行下载?
答案 0 :(得分:3)
您的“内容类型”标题似乎过度了。哦,你需要在exit()
之前实际发送文件。你所需要的只是
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="' . $xlsFile . '"');
readfile($xlsFile);
exit();
答案 1 :(得分:0)
请参阅此旧文章,了解要在上传时发送的正确MIME类型:
答案 2 :(得分:0)
您只需再添加一行
$xlsFile = 'test.xls';
header("Content-Type: application/vnd.ms-excelapplication/vnd.ms-excel");
header("Content-Disposition: attachment;filename='$xlsFile'");
header("Content-Transfer-Encoding: binary ");
echo file_get_contents($xlsFile);
exit();
答案 3 :(得分:0)
示例功能。
function file_download($filename, $mimetype='application/octet-stream') {
if (file_exists($filename)) {
// send headers
header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
// type
header('Content-Type: ' . $mimetype);
// date of modified
header('Last-Modified: ' . gmdate('r', filemtime($filename)));
header('ETag: ' . sprintf('%x-%x-%x', fileinode($filename), filesize($filename), filemtime($filename)));
// file size
header('Content-Length: ' . (filesize($filename)));
header('Connection: close');
header('Content-Disposition: attachment; filename="' . basename($filename) . '";');
// send file
echo file_get_contents($filename);
} else {
header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
header('Status: 404 Not Found');
}
exit;
}