【JavaScript前端通过文件流下载文件】文件流通过接口获取后调用以下方法模拟下载
/** * @description 文件流下载方法 * @param {文件流} blob * @param {文件名} fileName * @param {文件后缀名} fileType */downloadFile: function(blob, fileName, fileType) {var _fileType = fileType && fileType.toUpperCase();//转成大写var type = this.MIMITypes[_fileType] || "Application/octet-stream";//通过下方常用MIMITypes获得typevar url = URL.createObjectURL(new Blob([blob],{type: type}));//生成url对象var a = document.createElement("a");//创建a标签a.style.display = "none";//隐藏aa.href = https://www.isolves.com/it/cxkf/yy/js/2023-04-04/url;//赋值a标签的hrefa.download = fileName;//设置下载的文件名document.body.appendChild(a);//把a标签添加到页面上a.click();//模拟点击document.body.removeChild(a);//移除a标签URL.revokeObjectURL(url);//释放URL.createObjectURL创建的对象URL}//常用MIMITypesMIMITypes:{"TXT": "text/plain","css": "text/css","JPEG": "image/jpeg","JPG": "image/jpeg","PNG": "image/png","GIF": "image/gif","WEBP": "image/webp","SVG": "image/svg+xml","TIFF": "image/tiff","MPEG": "video/mpeg","MP4": "video/mp4","AVI": "video/x-msvideo","OGG": "video/ogg","MP3": "audio/mpeg","PDF": "application/pdf","PPT": "application/vnd.ms-powerpoint","PPTX": "application/vnd.openxmlformats-officedocument.presentationml.presentation","XML": "application/xml","CSV": "application/csv","ZIP": "application/zip","7Z": "application/x-7z-compressed","GZIP": "application/gzip","XLS": "application/vnd.ms-Excel","XLSX": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","DOC": "application/msword","DOCX": "application/vnd.openxmlformats-officedocument.wordprocessingml.document","": "application/octet-stream"}
文章插图
推荐阅读
- 文员|《女士的品格》:通过几个职场女性,让大家感受他们的魅力
- 前端视角下的转转客服通信过程
- |钓友们怎么通过网络学钓鱼知识?大家要学会取舍,避免被误导
- 应急调度指挥中心作用—通过哪些手段可以处理应急现场的指挥调度
- 张兰|张兰被证明真爱大s子女,曾通过信托给家产,汪小菲或早打算再生
- Python爬虫+数据分析之影评分析
- 值得考虑的七个出色的JavaScript项目
- 深入了解 JavaScript 内存泄漏
- JavaScript中根据字符串中的范围规则,判断当前值是否符合条件
- 前端面试题-工程化-webpack 编译流程