微信小程序—在线预览二进制流文件

编程入门 行业动态 更新时间:2024-10-15 10:12:38

微信小程序—<a href=https://www.elefans.com/category/jswz/34/1770935.html style=在线预览二进制流文件"/>

微信小程序—在线预览二进制流文件

// 请求 下载表格 接口exportExcel (e) {let that = thislet id = that.getPageOptions().idlet fileName = `【${that.data.holderInfo.nameC}】 ${that.data.bookingInfo.area}-${that.data.productInfopany}-${that.data.productInfo.plan}`wx.showLoading({title: '加载中...',mask: true})Util.httpRequest('Get', `${Config.exportExcel}/${id}/excel`, {}, 'arraybuffer').then(res => {console.log(res)wx.hideLoading()// that.createCatalog(res.data, fileName) // 创建存放目录}).catch(err => {console.log(err)})},// 创建存放目录createCatalog (arraybuffer, fileName) {const rootPath = wx.env.USER_DATA_PATHconst cachePath = rootPath + '/openFileCache'const manage = wx.getFileSystemManager()let newFileName = fileName + '.xlsx'// 判断是否已存在自定义目录manage.access({path: cachePath,success: res => {console.log(res)let readdir = manage.readdirSync(cachePath)console.log(readdir)for (let path of readdir) {manage.unlinkSync(cachePath + '/' + path)}this.openFileEv(cachePath, newFileName, arraybuffer)},fail: err => {console.log(err)// 新建自定义目录manage.mkdir({dirPath: cachePath,recursive: true,success: res => {console.log('创建成功')this.openFileEv(cachePath, newFileName)},fail: err => {console.log('创建失败')}})}})},// 打开文件openFileEv (cachePath, newFileName, arraybuffer) {const manage = wx.getFileSystemManager()// 显示加载wx.showLoading({title: '加载中...',mask: true})manage.writeFile({ // 写文件filePath: cachePath + '/' + newFileName, // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义data: arraybuffer,encoding: "binary", //二进制流文件必须是 binarysuccess: res => {// 打开文档wx.openDocument({filePath: cachePath + '/' + newFileName,showMenu: true,success: function (res) {console.log('打开文档成功')},fail: function () {console.log('打开失败')}})}})},

更多推荐

微信小程序—在线预览二进制流文件

本文发布于:2024-03-10 11:17:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1727826.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在线   文件   程序   微信小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!