我點擊了一個超鏈接按鈕,我想從資料庫中獲取影像并使用 laravel 和 vue js 在用戶端下載它。下面是我的腳本檔案代碼
getImage: function() {
axios.get('/getImage/' this.form.cashout_id )
.then(function (r)
{
const url = window.URL.createObjectURL(new Blob([r.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.' r.headers.ext); //or any other extension
document.body.appendChild(link);
link.click();
//hide loader
i.loader = false
})
.catch(function (error) {
alert('Error');
});
},
現在這是我正在獲取影像的控制器代碼。
public function getimage($id)
{
$cashout = CashOutDetail::findorfail($id);
$storage_date = Carbon::parse($cashout['recorded_date']);
return response()->download(
storage_path('app/cashoutdetails/'. $storage_date->year .'/' . $storage_date->format('M') . '/'. $cashout->bank_receipt),
'filename.jpg',
['Content-Type' => 'image/jpg']
);
}
問題是我的影像正在被獲取并顯示在控制臺視窗中,但無法下載。有人可以幫忙嗎?
uj5u.com熱心網友回復:
你應該試試:
axios({
method: 'GET',
url: '/getImage/123.jpg',
responseType: 'blob', // <-<<<<<<<<<<
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', '123.jpg');
document.body.appendChild(link);
link.click();
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/394580.html
下一篇:多個設備的RESTAPI身份驗證
