一、安装/引用image-tools
npm install image-tools
import { pathToBase64, base64ToPath } from "image-tools";
二、调用后台接口获取文档流乱码
getRestrictedConsumptionDatail({ url: item.filePath }).then((res) => {
this.openPdfFileByUrl(uni.arrayBufferToBase64(res));
});
res为获取到的文档流,需要使用uni.arrayBufferToBase64()将以下乱码转化为Base64编码:
三、处理编码:
openPdfFileByUrl(base64Data) {
let result = base64Data.replace(/[\r\n]/g, "");
let pdfBase64 = `data:application/pdf;base64,${result}`;
base64ToPath(pdfBase64)
.then((path) => {
uni.openDocument({
filePath: path,
success: function (FileRes) {
console.log("打开成功");
},
fail: (res) => {
console.log("打开失败");
},
});
})
.catch((error) => {
console.error(error);
});
},