js Blob下载功能实现

使用blob下载功能:

this.axios({

            responseType: 'blob',

            method: 'post',

            url: url,

            data: data

        })

        .then((res) => {

            if (res.status_code == 200||res.data.type=="application/zip") {

                let link = document.createElement('a');

                let blob = new Blob([res.data], {type: res.data.type});

                link.style.display = 'none';

                link.href = URL.createObjectURL(blob);

                link.download = filename; //下载的文件名

                document.body.appendChild(link);

                link.click();

                document.body.removeChild(link);

            } else {

  //              下载失败

                this.$message({

                    message: res.data.message || res.message || '下载失败!',

                    type: 'error'

                })

            }

        })

//请求失败

        .catch((res) => {

            console.log(res);

        });

你可能感兴趣的:(js Blob下载功能实现)