后端返回Base64文件流,前端显示文件(PDF、Image)

背景:

在实际开发中,后端返回的pdf文件,是以base64文件流的方式,返回给前端。前端需要将文件流进一步处理展示给客户。

一、使用VUE-PDF预览文件

使用Vue-pdf插件,首先要安装。在我之前的文章有提到。CMapReaderFactory可以帮助处理中文乱码的问题。

<template>
    <pdf v-for="i in numPages" :key="i" :src="src" :page="i" ref="PDFDom"></pdf>
</template>

import pdf from 'vue-pdf'
import CMapReaderFactory from 'vue-pdf/src/CMapReaderFactory.js'

getCarElectronicInfo({policyNo: this.policyNo}).then(res => {
    if(res.status == '0' && res.data) {
        let datas = 'data:application/pdf;base64,' + res.data.result //后端返回的文件流
        this.src = pdf.createLoadingTask({
		    url: datas,
			CMapReaderFactory
		});
        this.src.promise.then(pdf => {
		    this.numPages = pdf.numPages;
		});
    }
    
}).catch(error =>{
    console.log(error)
})

二、使用Window.URL.createObjectURL预览文件

getCarElectronicInfo({policyNo: this.policyNo}).then(res => {
    var binaryData = [];
    binaryData.push(res.data.result);
    var bolb = new Blob(binaryData, {type: "application/pdf"});
    var fileUrl = null;
    // 区分浏览器
    if (window.createObjectURL != undefined) { 
       fileURL = window.createObjectURL(blob)
    }else if(window.webkitURL != undefined){
        try {
            fileURL = window.webkitURL.createObjectURL(blob);
        }
    }else if(window.URL != undefined) {
        fileURL = window.URL.createObjectURL(blob);
    }
    let url= encodeURIComponent(fileURL)
    windoe.location.href = url
})

之前按照其他小伙伴的博客,使用pdf.js也做了一下但一直报错“PDF文件已损坏”。操作步骤是官网下载PDF.js,将其中的俩个文件,放置到本地项目的static文件夹下

 

 

下边是代码,请各位帮忙看下那里有问题

getCarElectronicInfo({policyNo: this.policyNo}).then(res => {
    if(res.status == '0' && res.data) {
        var binaryData = [];
        binaryData.push(res.data.result);
        this.url = window.URL.createObjectURL(new Blob(binaryData,{type:"application/pdf"}));
        window.open('/static/pdf/web/viewer.html?file=' +encodeURIComponent(this.url));
    }
})

三、如果后端直接返回PDF文件地址

简单粗暴的使用window.open(url),即可。如果在ios端,会遇到window.open失效的问题。这是因为ios有严格的安全模式;可用window.location.href代替

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端可以通过以下步骤将base64字符串转为文件并发送给后端: 1. 将base64字符串转为Blob对象 ```javascript const base64Str = 'data:image/png;base64,iVBORw0KGg....'; // 假设这是base64字符串 const arr = base64Str.split(','); const mime = arr[0].match(/:(.*?);/)[1]; // 获取文件类型 const bstr = atob(arr[1]); // 将base64字符串转为二进制字符串 let n = bstr.length; const u8arr = new Uint8Array(n); // 创建一个长度为n的Uint8Array类型数组 while (n--) { u8arr[n] = bstr.charCodeAt(n); // 逐位填充数组 } const blob = new Blob([u8arr], { type: mime }); // 将数组转为Blob对象 ``` 2. 将Blob对象转为文件 ```javascript const file = new File([blob], 'file.png', { type: mime }); // 将Blob对象转为File对象,用于上传 const reader = new FileReader(); reader.onload = function(e) { const fileStream = e.target.result; // 将File对象转为文件 // 发送文件后端 }; reader.readAsArrayBuffer(file); ``` 3. 发送文件后端 最后一步可以通过Ajax请求、Fetch API或WebSocket等技术实现。以Ajax为例: ```javascript const xhr = new XMLHttpRequest(); xhr.open('POST', '/upload', true); xhr.setRequestHeader('Content-Type', 'application/octet-stream'); xhr.send(fileStream); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { console.log('上传成功'); } }; ``` 注意,上传文件时需要设置请求头的Content-Type为application/octet-stream。当然,具体的请求头可能需要根据后端的要求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值