微信小程序pdf预览

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) 打印的内容:

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

相关推荐
游戏开发爱好者81 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_915106323 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106323 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息4 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”5 小时前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
Java面试题总结18 小时前
基于 Java 的 PDF 文本水印实现方案(iText7 示例)
java·python·pdf
傻啦嘿哟20 小时前
Python操作PDF页面详解:删除指定页的完整方案
开发语言·python·pdf
换日线°1 天前
NFC标签打开微信小程序
前端·微信小程序
光影少年1 天前
AIGC + Taro / 小程序
小程序·aigc·taro
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview