1.示例图
2.代码
fileId:要预览的pdf文件的id
viewsFiles(fileId) {
wx.showLoading({
title: '加载中...'
});
var params = {
url: "/common/getFile/" + fileId ,//后端提供的接口
method: "GET",
responseType: "arraybuffer",
callBack: (res) => {
console.log(res)
const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器
fs.writeFile({
// 写文件
filePath: wx.env.USER_DATA_PATH + "/" + '文件名.pdf', // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
data: res,
encoding: "binary", //二进制流文件必须是 binary
success(res) {
wx.openDocument({
// 新开页面打开文档
filePath: wx.env.USER_DATA_PATH + "/" + '文件名.pdf', //拿上面存入的文件路径
showMenu: true, // 是否显示右上角菜单(3个点)
success: function (res) {
setTimeout(() => {
wx.hideLoading();
}, 500);
},
});
},
});
}
}
http.request(params)
},
console.log(res) 打印的内容:

后端接口返回的内容样式:
