uniapp小程序下载文件并打开

250 阅读1分钟

页面代码

通过接口(option.params.orderId 传参)获取文件的后缀名 判断是开发环境还是线上环境 通过uni.downloadFile uni.openDocument 下载并打开文件

uni.openDocument新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。

if (process.env.NODE_ENV === 'development') {
    var url = 'https://s.puyanglife.cn';
} else {
    var url = 'https://s.puyanglife.cn';
}
uni.downloadFile({
    url: url + path,
    success: res => {
    uni.openDocument({
        ilePath: res.tempFilePath,
        fileType: 'pdf'
        })
    },
    fail:err => {
        uni.showToast({
        title: '文件打开失败',
        icon: "none"
        })
    }
})

微信公众号配置

配置白名单 下载文件域名

image.png