美文网首页
js流文件下载和生成预览链接

js流文件下载和生成预览链接

作者: coderfl | 来源:发表于2023-05-11 10:43 被阅读0次
    // 附件预览下载
        downloadFile: params => {
            return new Promise((resolve, reject) => {
                axios({
                    method: "get",
                    url: `${process.env.VUE_APP_BASE_API}/common/downloadFile`, // 请求地址
                    params, // 参数
                    responseType: "blob", // 表明返回服务器返回的数据类型
                    headers: {
                        Authorization: sessionStorage.getItem('loginToken') || ''
                    }
                }).then(value => {
                    if (params.openOnLine) {
                        let binaryData = [];
                        binaryData.push(value.data);
                        const previewSrc = window[window.webkitURL ? 'webkitURL' : 'URL'].createObjectURL(new Blob(binaryData));
                        resolve(previewSrc)
                    } else {
                        let MIME = {
                            jpg: 'image/jpeg',
                            jpeg: 'image/jpeg',
                            png: 'image/png',
                            webp: 'image/webp',
                            xls: 'application/vnd.ms-excel',
                            xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                            doc: 'application/msword',
                            docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                            pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
                            ppt: 'application/vnd.ms-powerpoint',
                            pdf: 'application/pdf'
                        };
                        let blob = new Blob([value.data], {
                            type: MIME[params.fileType]
                        });
                        let fileName = params.fileName;
                        if (window.navigator.msSaveOrOpenBlob) {
                            navigator.msSaveBlob(blob, fileName);
                        } else {
                            let link = document.createElement('a');
                            link.href = window.URL.createObjectURL(blob);
                            link.download = fileName;
                            link.click();
                            // 释放内存
                            window.URL.revokeObjectURL(link.href);
                        }
                    }
                }).catch(err => {
                    reject(err);
                });
            })
        },
    

    相关文章

      网友评论

          本文标题:js流文件下载和生成预览链接

          本文链接:https://www.haomeiwen.com/subject/unrssdtx.html