2023-03-12封装下载附件的方法

1.需要后端提供一个接口

//方式一

// let url = 'xxxxxxxxxx/downLoadPlanExcel';

// window.location.href=url;

//方式二

// let url = 'xxxxxxxxx/downLoadPlanExcel';

// let div = document.createElement("div");

// div.innerHTML =

//    '<iframe id="idFrame" name="idFrame" src="' +

//    url +

//    '" height = "0" width = "0" frameborder="0" scrolling="auto" style = "display:none;visibility:hidden" ></iframe>';

// document.body.appendChild(div);

//

// 方式三

handlePreview(fileInfo) {

            let params = {}

            let url = (CURRENT_URL + this.$CST.API_TASK_FILE_DOWNLOAD).replace('{fileId}', fileInfo.attachFileId)

            let request = {

                url,

                data: params,

                method: 'get',

                headers: {

                'Authorization': getCookie('token')

                },

                responseType: 'blob',

            }

            axios(request).then(res => {

                if (res.status == 200) {

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

                if (window.navigator.msSaveOrOpenBlob) {

                    navigator.msSaveBlob(blob, fileInfo.fileName)

                } else {

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

                    link.href = window.URL.createObjectURL(blob);

                    link.download = fileInfo.fileName;

                    link.click();

                    window.URL.revokeObjectURL(link.href)

                }

                }

            }).catch((reason) => {

                this.$notify({

                title: '提示',

                message: '失败' + reason,

                duration: 0

                });

            })

        },

参考:https://blog.csdn.net/MoXinXueWEB/article/details/126092614 前端实习下载的几种方法

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容