【发布时间】:2022-01-14 17:21:23
【问题描述】:
我的文件存储在storage folder,当我点击下载按钮到download image file,un willingly .txt file is downloading包含响应数据。
我怎样才能让这段代码从存储文件夹中下载图像。
SQL 列数据:
file.jpg in file_name column
123 in custom_code column
控制器:
public function getdata(Request $request)
{
$files = Files::where('custom_code','=',$request->id)->first();
$image_name= Files::where('custom_code','=',$request->id)->pluck('file_name');
$image = Storage::download('uploads',$image_name);
return response()->json([
'files'=> $files,
'image'=> $image,
], 200);
}
VUE:
function download(){
axios.get("http://127.0.0.1:8000/api/get-data/"+state.input_code, {responseType: 'blob'})
.then(response => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', response.data.file_name);
document.body.appendChild(link);
link.click();
})
.catch(e => {
console.log(e);
});
}
HTML:
<button @click="download()">Download</button>
为什么要下载包含响应数据而不是图像的文本文件。
【问题讨论】:
标签: jquery laravel vue.js laravel-8 vuejs3