簡體   English   中英

Axios 從返回的 url 下載文件

[英]Axios download file from returned url

我返回的數據如下:

data: "C:\laragon\www\test\public\exports\20200501030547-Products.xlsx"
message: "Products are successfully exported."

我想開始自動下載C:\laragon\www\test\public\exports\20200501030547-Products.xlsx文件,因為它在成功部分返回。

代碼

axios.get( '/api/admin/products/export', {
    headers: {
        Authorization: localStorage.getItem('access_token')
    }
}).then(res => {
    console.log(res.data.data) // my file
    this.$notify({
        title: 'Hooray!',
        message: res.data.message,
        offset: 100,
        type: 'success'
    });
})
.catch(error => {
    var errors = error.response.data;
    let errorsHtml = '<ol>';
    $.each(errors.errors,function (k,v) {
        errorsHtml += '<li>'+ v + '</li>';
    });
    errorsHtml += '</ol>';

    this.$notify.error({
        title: 'Export Error',
        dangerouslyUseHTMLString: true,
        message: errorsHtml
    });
});

任何想法?

解決了

解決方案是使用window.open('res.data.data', '_blank')但是如果您在 chrome 瀏覽器中使用本地數據(就像我現在一樣),它會返回此錯誤Not allowed to load local resource:

但我想它在真實服務器中的生產應用程序應該沒有任何問題。

  • 希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM