我尝试从服务器从laravel下载任何文件,但所有文件都是以.txt文件的形式下载的

这是我的javascript代码:
downloadAttachment:function (id){
axios({
url: '/api/user/downloadFile/'+id,
method: 'GET',
responseType: 'blob',
}).then((response) => {
let fileURL = window.URL.createObjectURL(new Blob([response.data]));
let fileLink = document.createElement('a');
fileLink.href = fileURL;
fileLink.setAttribute('download', response.data.type);
document.body.appendChild(fileLink);
fileLink.click();
});
}拉威尔:
public function downloadDocument($file)
{
$path=$file->src;
if(Storage::exists($path))
{
$file=Storage::get($path);
$type=Storage::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
abort(404);
}发布于 2021-06-11 01:47:00
请改用Storage::download:
public function downloadDocument($file)
{
$path = $file->src;
if (Storage::exists($path)) {
return Storage::download($path);
}
abort(404);
}这是docs。
发布于 2021-06-14 03:38:34
呃..。为什么要浪费时间用axios将其作为blob下载,然后保存呢?为什么不直接下载fileLink.href = url,而不浪费任何内存呢?
似乎你要做的一切都是这样的:
location = '/api/user/downloadFile/' + id或者这样:
<a href="/api/user/downloadFile/123">https://stackoverflow.com/questions/67923218
复制相似问题