我编写了一个脚本,该脚本在浏览器中显示来自服务器的一些pdf文件。这是正常的,如果用户单击“保存”,则会在对话框窗口中显示正确的文件名。但是Chrome无法显示正确的文件名。
请看图片,然后您知道我的意思:网址:... / api / downloads / getFile / MDBhYmYzZDI2MUFFUCBIb...。Screenhot Chrome wrong Filename
我的PHP代码:
<?php
$fileNameOnServer = "MDBhYmYzZDI2MUFFUCBIb.pdf";
$file = "/media/downloads/pdf/".$fileNameOnServer;
$originalFileName = getFileNameFromSQL($fileNameOnServer);
header("Content-type: application/pdf");
header("Content-length: ".filesize($file)."");
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: inline; filename="'.rawurldecode($originalFileName).'"');
readfile($file);
exit;
?>
有关文件名的信息:有很多pdf文件,为避免重复,我对所有文件名进行了哈希处理并将原始名称存储在数据库中。
编辑:
我已经使用函数“ $ originalFileName = getFileNameFromSQL($ fileNameOnServer);”更新了代码。
此屏幕快照显示的是“另存为”对话框窗口,其中包含正确的FileName:enter image description here
来自MDN:
filename
后跟一个包含所传输文件的原始名称的字符串。文件名始终是可选的,并且不能被应用程序盲目使用:路径信息应被删除,并且应完成向服务器文件系统规则的转换。此参数主要提供指示性信息。 与Content-Disposition: attachment
结合使用时,它用作向用户显示的最终“另存为”对话框的默认文件名。您正在使用
Content-Disposition: inline
。因此,浏览器将忽略filename
参数。
一种解决方法是制作URL:
/path/$fileNameOnServer/$originalFileName