Vue axios request binary stream excel file, response garbled

:axiospostexcelresponse


response


forced text opening or garbled

May.28,2021

Last update:

since the landlord has adopted my answer, but I still want to talk about some questions carefully. For this reason, I have written the following code. For convenience, I directly download a xls that exists in the root directory of the server.

axios.get('http://127.0.0.1/1.XLS', {
                responseType: 'blob' //blob
            })
            .then(response => {
                console.log(response);//response
                let url = window.URL.createObjectURL(response.data);
                console.log(url)
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.href = url;
                        a.download = '2.xls';
                        a.click();
                        window.URL.revokeObjectURL(url);
            })
            .catch(err => {
                console.log(``);
               
                console.log(err);
            })


responsedata blob Object

createObjectURL blob File

URL


response.data blob Object let url = window.URL.createObjectURL(new Blob([res.data]))

:
1. responseType: 'blob' responseType
2.axios

:

ExcelExcel

:
responseType: 'arraybuffer' responseType: 'blob'

let objectUrl = URL.createObjectURL(res.data);


responsetextexcelresponse



responseType: 'blob' blob {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"} excel


postman...




mockjsrequire('mockjs')
mock
image
mockjs1
image


1.office
2.QQpostmanoffice
3.wps


image

Menu