【发布时间】:2020-08-27 11:12:39
【问题描述】:
我正在制作 PDF 的 zip 文件,然后在浏览器中下载它。我使用了 ajax 并希望收到文件作为响应,以便我可以下载它。
return response()->download(public_path().'/documents/'.$zipName , $zipName, $headers);
this is the response i m getting
我也尝试过这种方法,但它不知道我错过了什么。
Laravel 响应:
$downloadData = [
'zipFile' => $zipName,
'headers' => $headers,
'url' => public_path('documents/').$zipName
];
return response()->json(['status'=>1,'data'=> $downloadData]);
AJAX 成功:
var a = document.getElementById('zipDownload');
a.href = data.data.url;
a.target = '_blank';
a.download = data.data.zipFile;
setTimeout(function() {
a.click();
}, 200);
当我将其粘贴到新选项卡中时,锚显示文件和下载的正确路径.. 但是当我触发锚标记时,它显示 不允许加载本地资源:file:///C:/xampp/htdocs/ems/public/documents/records-20200512002210.zip
【问题讨论】:
-
你能在
response()->download()上面试试header('Content-Type: ' . mime_content_type($filepath));吗?
标签: ajax laravel file download response