简体   繁体   English

Axios 从返回的 url 下载文件

[英]Axios download file from returned url

I have returning data like:我返回的数据如下:

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

I want to start download C:\laragon\www\test\public\exports\20200501030547-Products.xlsx file automatically as it returns in success part.我想开始自动下载C:\laragon\www\test\public\exports\20200501030547-Products.xlsx文件,因为它在成功部分返回。

Code代码

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
    });
});

Any idea?任何想法?

Solved解决了

Solution was using window.open('res.data.data', '_blank') however if you use local data in chrome browser (as i am right now) it returns this error Not allowed to load local resource:解决方案是使用window.open('res.data.data', '_blank')但是如果您在 chrome 浏览器中使用本地数据(就像我现在一样),它会返回此错误Not allowed to load local resource:

But I guess it shouldn't have any issue with production app in real server.但我想它在真实服务器中的生产应用程序应该没有任何问题。

  • hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM