我有一个脚本,可以生成一个文件以通过ajax 下载。我需要控制这个请求,因为不是每个人都应该能够下载文件。该过程如下所示:
$filename,array_shift并将其余内容放入$content变量中,$content变量。不知怎的,这并没有按预期工作。这不是因为 iframe,因为当我在浏览器中访问该 URL 时,Chrome 告诉我有错误ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION。我正在使用 Laravel,但我没有看到我在哪里设置了错误的标头?
到目前为止的下载脚本:
public function download($fileId)
{
$file = $this->tempFilesPath . $fileId;
if (file_exists($file)) {
$data = explode("\n", file_get_contents($file));
//@unlink($file);
$fileName = array_shift($data);
$content = implode("\n", $data);
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename=' . $fileName);
echo $content;
exit;
}
}
Run Code Online (Sandbox Code Playgroud)
$fileName转储和的值$content显示预期值。
建议?谢谢。
经过一番额外的挖掘后,我发现了一篇关于这个特定错误的帖子。这是因为我用日期格式化文件名,其中包含逗号,这让 Chrome 感到不安。更改了文件名约定,现在可以使用了。如果其他人稍后可能遇到这种行为,我会将问题留在网上。
对于未来的解决方案寻求者:
我首先将文件命名为$filename = 'download_' . date('d-m-Y, H:i:s') . '.ext',但日期格式中的逗号是问题的原因。忽略它,您应该更进一步解决您的问题。