当前位置: 首页 > news >正文

axios接收zip文件文件

问题代码

async previewLoadRequest(end){const response = await axios.get('/api2/pdfToImage',  {params: { path: this.file.path ,startPage: this.endPage, // 开始页码endPage: end , // 结束页码},});if(response.status != 200){return false}console.log('response',response)console.log('response.data',response.data)// 2. 解压ZIP文件const zip = new JSZip();// const zipData = await zip.loadAsync(response.data);const blob = new Blob([response.data], {type: 'application/zip'});const zipData = await zip.loadAsync(blob);for (const [name, file] of Object.entries(zipData.files)) {console.log('name',name)if (!file.dir) { // 跳过目录const blob = await file.async('blob');const url = URL.createObjectURL(blob);this.urls.push(url);}}return true
},

抛出错误

fileCard.vue:154 处理失败: Error: Corrupted zip: 
missing 40318 bytes. at h.readEndOfCentral 
(jszip.min.js:13:1) at h.load (jszip.min.js:13:1) 
at eval (jszip.min.js:13:1) 
at async Proxy.previewHandler (fileCard.vue:138:1)

首要排查的是是否没加上responseType: 'blob' 或 responseType: 'arraybuffer'​ 导致的二进制数据接收异常。

加上后解决

async previewLoadRequest(end){const response = await axios.get('/api2/pdfToImage',  {params: { path: this.file.path ,startPage: this.endPage, // 开始页码endPage: end , // 结束页码},responseType: 'blob' // 设置响应类型为 Blob非常重要不能省略});if(response.status != 200){return false}console.log('response',response)console.log('response.data',response.data)// 2. 解压ZIP文件const zip = new JSZip();// const zipData = await zip.loadAsync(response.data);const blob = new Blob([response.data], {type: 'application/zip'});const zipData = await zip.loadAsync(blob);for (const [name, file] of Object.entries(zipData.files)) {console.log('name',name)if (!file.dir) { // 跳过目录const blob = await file.async('blob');const url = URL.createObjectURL(blob);this.urls.push(url);}}return true
},

相关文章:

  • 2025 中青杯数学建模AB题
  • AUTOSAR图解==>AUTOSAR_SRS_LIN
  • 前端实战:用 JavaScript 模拟文件选择器,同步实现图片预览与 Base64 转换
  • SQLAlchemy 2.0 查询使用指南
  • laravel中模型中$fillable的用法
  • AI方案调研与实践二:模型训练
  • 【 大模型技术驱动智能网联汽车革命:关键技术解析与未来趋势】
  • PHP学习笔记(九)
  • 10大Python知识图谱开源项目全解析
  • 白杨SEO:做AI搜索优化的DeepSeek、豆包、Kimi、百度文心一言、腾讯元宝、通义、智谱、天工等AI生成内容信息采集主要来自哪?占比是多少?
  • Spring Boot 中修改 HTTP 响应状态码(即 `response.status`)可以通过以下几种方式实现
  • 新能源产业破局之道:达索 3DE(PLM)系统重构数据管理与工程变更效率
  • 什么是数据分析
  • 2025年 中青杯A题论文发布
  • Redis学习打卡-Day6-Redis 高可用(上)
  • linux debug技术
  • el-form 使用el-row el-col对齐 注意事项
  • 后端开发概念
  • 【MPC控制 - 从ACC到自动驾驶】车辆纵向动力学建模与离散化:MPC的“数字蓝图”
  • 微前端架构:从单体到模块化的前端新革命
  • 怀化网站优化公司哪家好/网站查找工具
  • 外贸网站建站注意事项/seo推广费用
  • 成绩分析智能网站怎么做/专业seo网络推广
  • 茶网站设计素材下载/最近时政热点新闻
  • 做网站卖掉/公司推广
  • 网页设计师工作职责/江门seo推广公司