vue+axios 백그라운드에서 되돌아오는 파일 흐름 다운로드

1483 단어
// 
axios.interceptors.response.use(response => {// 
    NProgress.done()
    if(response.headers['content-disposition']){// content-disposition 
        return response
    }else{
        return response.data
    }
}, error => {
    Message.error(' , ');
    return Promise.reject(error.response);
});

//http 
postBlob(url){
    return axios({
        method: "post",
        baseURL: localhosts,
        url,
        headers: {
            //    Authorization
            Authorization: sessionStorage.getItem('token'),
            "X-Auth-Token": sessionStorage.getItem('xAuthToken')
        },
        responseType:"blob"
    })
    .then(res => {
        return res;
    },
    err => {
        Message.error(' ');
        return err;
    });
},

// 
download(){
    //this.api.downloadOrder 
    this.http.postBlob(this.api.downloadOrder)
    .then(res => {
        if(res){
            var fileName = decodeURI(res.headers['content-disposition'].split('=')[1]);
            var blob = new Blob([res.data]);
            if(window.navigator.msSaveOrOpenBlob){
                navigator.msSaveBlob(blob,fileName);
            }else{
                var a = document.createElement("a");
                a.download = fileName;
                a.href = window.URL.createObjectURL(blob);
                a.click();
            }
        }
    })
},

좋은 웹페이지 즐겨찾기