我正在创建 pdf 文档以供下载,例如有人单击 PDF 链接,然后生成 pdf,浏览器会打开带有该 pdf 文件路径的新窗口。问题是浏览器在创建后大约 40-50 秒内为该文件提供 404 NOT found 错误,但在那之后,当我刷新浏览器时,该文件存在以供查看或下载。
一个 pdf 链接是http://images.myvouchercodes.co.uk/mvclocal/pdf/ca3b5098-9b35-7d8e.pdf ,您可以在其中查看文件,但相同的 url 在创建后立即找不到 404。我正在使用以下代码编写文件
try{
$fh = fopen($filename, "w");
$contents = $this->render(); // return pdf contents in string
if(fwrite($fh, $contents))
{
$fh = fopen($filename, "r");
while(strlen(file_get_contents($filename)) != strlen($contents))
{ }
echo $filename;
}
else
{
throw new Exception ("Unable to create pdf");
}
fclose($fh);
}
catch(Exception $e)
{
echo $e->getMessage();
}
该调用是 ajax,它在 pdf 完成时回显文件名,然后将此文件名附加到 url,我使用 window.open() 打开带有 pdf 链接的新窗口,这给了我 404 not found 错误。有谁知道为什么会发生这个错误?