The file downloaded by vue+axios through POST request is garbled.

units are now separated from each other, and then get stuck in downloading files. Sincerely ask for advice or give directions

.

Baidu Google and everything have been searched for a long time. I don"t know if the keyword is wrong or everyone"s environment is different. It seems that the code that passes through axios is not successful. The back-end (java) wrote api"s little partner said that there will be no problem if you click on the request and download it in this way on the page that is not separated before. It has been bothering you for a long time. Please answer the puzzle

.

related codes

methods: {

downloadExcel(id){
    const data = {}
    data.id = id
    data.token = this.$store.state.token
    this.axios({
        method: "POST",
        url: "-sharp-sharp-sharp",
        data: qs.stringify(data),
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        responseType: "arraybuffer"
    }).then(function(res) {
        let blob = new Blob([res], {type: "multipart/form-data"}); 
        let objectUrl = URL.createObjectURL(blob); 
        window.location.href = objectUrl;  
    });

}

}

< H2 > the following picture is the information I requested and returned < / H2 >



Nov.02,2021

var blob = new Blob([res.data], {type: 'application/vnd.ms-excel;charset=utf-8'}); //vnd.ms-excel
var downloadElement = document.createElement('a');
var href = window.URL.createObjectURL(blob); //
downloadElement.href = href;
downloadElement.download = 'xxx.xls'; //
document.body.appendChild(downloadElement);
downloadElement.click(); //
document.body.removeChild(downloadElement); //
window.URL.revokeObjectURL(href); //blob

it should be OK to write like this


use blob ()


the so-called download is to get a file address for the front end, and you should ask the back end to return an address to you.

Menu