小工具知识网

当前位置: 首页 > 行业知识 > uniapp 在线预览各种格式文件(支持doc, xls, ppt, pdf, docx, xlsx, pptx格式) 适用于小程序 (解决了真机调试可以打开,发布体验版打不开的问题)(可设置文件名)

行业知识

uniapp 在线预览各种格式文件(支持doc, xls, ppt, pdf, docx, xlsx, pptx格式) 适用于小程序 (解决了真机调试可以打开,发布体验版打不开的问题)(可设置文件名)

2026-11-03 23:08:43 来源:互联网转载

代码:

uni.downloadFile({  url: 'https://example.com/somefile.pdf', //自定义的文件地址  filePath: uni.env.USER_DATA_PATH + "/" + "隐私协议.docx",  //设置文件名  success: function (res) {    var filePath = res.filePath || res.tempFilePath;    uni.openDocument({      filePath: filePath,      showMenu: true,      success: function (res) {        console.log('打开文档成功');      }    });  }});

参考官方文档:

uni.saveFile(OBJECT) @savefile | uni-app官网uni-app,uniCloud,serverlesshttps://uniapp.dcloud.net.cn/api/file/file.html#opendocument

tips:

问题一: 打不开

        1. 文件地址须保证能正常打开(在浏览器中尝试是否能预览或正常下载)

        2.注意文件名最好为数字字母(非中文)

问题二: 真机调试可以打开,体验版没反应:

        1.小程序后台-开发设置中-设置downloadFile合法域名(文件地址域名)

        2.问题一不能出现

问题三: 文件名是一长串字符,需要设置文件名:

        1. filePath: uni.env.USER_DATA_PATH + "/" + "隐私协议.docx",  //设置文件名

        2. var filePath = res.filePath || res.tempFilePath

上一篇:微软架设服务器怎么用?

下一篇:php网站怎么搭建环境配置?