vue2 解密图片地址(url)-使用blob文件-打开png格式图片

一、背景

开发中需要对加密文件进行解码,如图片等静态资源。

根据后端给到的url地址,返回的是图片文件,但是乱码的,需要解码成png图片进行展示

二、请求接口

将后端返回的文件转为文件流, 创建Blob对象来存储二进制数据,通过URL.createObjectURL()方法创建二进制数据的URL,将URL赋值给imgData以显示图片。

javascript 复制代码
 //初始化图片数据并进行处理
    initImg() {
      this.isLoading = true
      //请求携带的参数
      let param = {
        printType: this.printType,
        orderIdList: Array.isArray(this.orderIdList) ? this.orderIdList : this.orderIdList.split(','),
        billTypeList: this.billType.split(',')
      }
      let promise
      //发请求,此处是封装的请求
      promise = this.$http.any('post', `${this.$url.payList.getPrintReceipt}`, {}, param, null, 'blob')
      // 发起POST请求,获取打印收据的二进制数据,并将结果赋值给promise变量
      promise
        .then((res) => {
          let reader = new FileReader() // 创建FileReader对象,用于读取二进制
          let response = null
          reader.onload = () => {
            // 如果读取的结果是以'{'开头,则将其解析为JSON对象
            if (reader.result[0] == '{') {
              response = JSON.parse(reader.result)
              this.$message.error(response.msg)
            } else {
              this.inChooseCharge = false
              let fileType = 'png' // 设置文件类型为png
              let blob = new Blob([res], { type: `application/${fileType}` }) // 创建Blob对象,用于存储二进制数据
              let data = window[window.webkitURL ? 'webkitURL' : 'URL']['createObjectURL'](blob) // 通过URL.createObjectURL()方法创建二进制数据的URL
              this.imgData = data // 将URL赋值给imgData,用于显示图片
              console.log('printinitImg -- imageData', this.imgData)

              let img = new Image()
              // 当图片加载完成时执行回调函数
              img.onload = () => {
                //对图片宽高比进行判断
                if (img.width / img.height >= 1100 / 595) {
                  this.fitWidth = true
                } else {
                  this.fitWidth = false
                }
                this.$nextTick(() => {
                  this.popupDoLayout()
                })
              }
              img.src = this.imgData
            }
            this.isLoading = false
          }
          reader.readAsText(res, 'utf-8') // 以文本格式读取二进制数据
        })
        .catch((error) => {
          this.isLoading = false
          console.log('请求失败:', error)
        })
    }

说明:发请求时要说明是转换成blob ,再创建Blob对象,存储二进制数据。最主要的还是要多打日志,看每一步的结果,再做对应操作!!!

MDN文件参考:👉URL.createObjectURL() - Web API 接口参考 | MDN

三、结构

使用img展示图片,利用element的image-viewer来进行图片缩放,需要从element中引入

javascript 复制代码
<div class="main print-box" :class="{ 'fit-width': fitWidth, 'fit-height': !fitWidth }" v-else>
      <img class="img" :src="imgData" alt="预览图" @load="popupDoLayout" @click="imgData ? getShowViewer(imgData) : null" />
      <div class="flex bottom-box">
        <el-button type="primary" @click="print" :disabled="!imgData">打印</el-button>
      </div>
    </div>
    <el-image-viewer v-if="showViewer" :on-close="closeViewer" :url-list="viewerImgList" />

从element中引入image-viewer使用👇

相关推荐
archko3 小时前
android pdf框架-15,mupdf工具与其它
android·pdf
东风西巷7 小时前
MobiPDF安卓版(PDF阅读编辑工具) 修改版
学习·pdf·电脑·软件需求
qq_205279057 小时前
Unity 项目外部浏览并读取PDF文件在RawImage中显示,使用PDFRender插件
pdf
郑..方..醒12 小时前
java实现ofd转pdf
java·pdf
_小许_1 天前
Go语言实现HTML转PDF
golang·pdf·html
2501_930707781 天前
使用C#代码将ODT转换为PDF文件
pdf
搞什么滚去学习1 天前
在昇腾910B服务上部署搭建适配PDF解析工具Mineru2.5开源项目
pdf
傻啦嘿哟2 天前
Python高效实现Excel转PDF:无Office依赖的轻量化方案
python·pdf·excel
CodeCraft Studio2 天前
CAD文件处理控件Aspose.CAD教程:在 Python 中将 SVG 转换为 PDF
开发语言·python·pdf·svg·cad·aspose·aspose.cad
CodeCraft Studio3 天前
国产化PDF处理控件Spire.PDF教程:C#中轻松修改 PDF 文档内容
前端·pdf·c#·.net·spire.pdf·编辑pdf·修改pdf