Vue

브라우저 blob 데이터 받기

SO-BBANG 2019. 10. 30. 13:36

blob type vnd.ms-excel 데이터 받기

 

ie는 ‘msSaveOrOpenBlob’라는 파일을 만들고 다운로드하기위한 자체 API가 있어서 그걸써야한다.

 

this.$http.instance().post(url, domain, {responseType:'blob',headers: {"Content-Type": "application/json"}}).then((response)=>{
    console.log("성공");
    const filename = util.replaceAll(decodeURI(response.headers['content-disposition'].split("filename=")[1]), '"', '');

    // ie 10+
    if(window.navigator.msSaveBlob) {
        const blob = new Blob([response.data]);
        window.navigator.msSaveBlob(blob, filename);
    } else { // not ie
        const blob = window.URL.createObjectURL(new Blob([response.data], { type: 'vnd.ms-excel' })); // ms-excel 데이터 받을거임
        const link = document.createElement('a');
        link.href = blob;
        link.setAttribute('download', filename);
        document.body.appendChild(link);
        link.click();
    }
}).catch(err=>{
    console.log("excel 실패",err);
});