当用户点击链接时,我真的很难让我的应用程序打开pdf.
到目前为止,锚标记重定向到一个页面,该页面发送以下标题:
$filename='./pdf/jobs/pdffile.pdf;
$url_download = BASE_URL . RELATIVE_PATH . $filename;
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='$filename");
readfile("downloaded.pdf");
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用,有没有人过去成功地解决了这个问题?
gat*_*gat 118
w3schools上的示例2 显示了您要实现的目标.
Run Code Online (Sandbox Code Playgroud)<?php header("Content-type:application/pdf"); // It will be called downloaded.pdf header("Content-Disposition:attachment;filename='downloaded.pdf'"); // The PDF source is in original.pdf readfile("original.pdf"); ?>
还要记住,
重要的是要注意在发送任何实际输出之前必须调用header()(在PHP 4及更高版本中,您可以使用输出缓冲来解决此问题)
小智 24
$name = 'file.pdf';
//file_get_contents is standard function
$content = file_get_contents($name);
header('Content-Type: application/pdf');
header('Content-Length: '.strlen( $content ));
header('Content-disposition: inline; filename="' . $name . '"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
echo $content;
Run Code Online (Sandbox Code Playgroud)
小智 8
我最近遇到了同样的问题,这对我有帮助:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="FILENAME"');
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("PATH/TO/FILE"));
ob_clean();
flush();
readfile(PATH/TO/FILE);
exit();
Run Code Online (Sandbox Code Playgroud)
我在这里找到了这个答案
您的代码中需要考虑一些事项.
首先,正确编写这些标题.你永远不会看到任何服务器发送Content-type:application/pdf
,标题Content-Type: application/pdf
,间隔,大写的第一个字母等.
在文件名中Content-Disposition
仅是文件名,而不是完整路径,并altrough我不知道,如果它的强制性与否,这个名字来源于包裹在"
不'
.此外,你的最后一个'
失踪.
Content-Disposition: inline
暗示文件应该显示,而不是下载.请attachment
改用.
此外,将文件扩展名设为大写,以使其与某些移动设备兼容.
总而言之,您的代码看起来应该更像这样:
<?php
$filename = './pdf/jobs/pdffile.pdf';
$fileinfo = pathinfo($filename);
$sendname = $fileinfo['filename'] . '.' . strtoupper($fileinfo['extension']);
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=\"$sendname\"");
header('Content-Length: ' . filesize($filename));
readfile($filename);
Run Code Online (Sandbox Code Playgroud)
Content-Length
是可选的,但如果您希望用户能够跟踪下载进度并检测下载是否中断,这也很重要.但在使用它时,您必须确保不会随文件数据一起发送任何内容.确保之前<?php
或之后绝对没有?>
,甚至没有空行.
归档时间: |
|
查看次数: |
242281 次 |
最近记录: |