uniapp小程序在线预览
文章目录
- 此文章主要为自己的笔记,所以直接上代码
此文章主要为自己的笔记,所以直接上代码
代码如下:
chakan() {let that = thisuni.downloadFile({url: that.fileUrl, // 必须是 HTTPS 地址(小程序要求)success: function(res) {const tempFilePaths = res.tempFilePath;console.log(tempFilePaths,'tempFilePaths');uni.openDocument({filePath: tempFilePaths,fileType: that.getFileType(that.fileUrl), // 显式指定类型(如 'pdf', 'docx', 'xlsx' 等,如果不设置类型,那么在手机上就会显示:此文件为unknown文件。所以这个是必填的success: function(res) {console.log('打开文档成功');},fail: function(err) {console.log('打开文档失败', err);}});}});},//判断文件类型getFileType(fileName) {const extension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();return extension || 'unknown';},