javascript - axios 下载每个带有 txt 扩展名的文件
问题描述
我尝试从 laravel 的服务器下载任何文件,但所有文件都下载为 .txt 文件
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);
}
解决方案
改用Storage::download
:
public function downloadDocument($file)
{
$path = $file->src;
if (Storage::exists($path)) {
return Storage::download($path);
}
abort(404);
}
这是文档。
推荐阅读
- c - 仅在发布时分配失败
- python - 关闭张量流消息
- python - 将烧瓶应用程序部署到 Elastic Beanstalk 时出错
- java - 没有单击按钮就不会传递 EditText 数据
- angular - 两个组件之间无法通信,侧边栏未更新
- java - 如何在 Java 应用程序中精细控制多个线程?
- angular - Firebase 异步数据检索需要时间
- c# - 如何在 C# 中修复“Office.Interlop -2146822384:无法编辑范围”
- r - 有没有办法选择一个因素的水平进行分析
- java - Camel SFTP 组件 - SSH 私钥 URI 适用于 privateKeyFile,不适用于 privateKey