首先要在小程序后台配置 downloadFile 合法域名

Snipaste_2022-06-24_13-59-50.png

然后使用 uni.downloadFile 实现文件预览,代码如下

uni.downloadFile({
    url: "你要预览的文件地址", 
    success: (res) => {
        if (res.statusCode === 200) {
            // 使用uni.saveFile获取文件临时路径
            uni.saveFile({
                tempFilePath: res.tempFilePath,
                success: function (save) {
                    // 自动打开手机预览文件页面
                    uni.openDocument({
                        filePath: save.savedFilePath,
                        success: function (open) {
                            // 打开文件成功
                            console.log(open)
                        }
                    })
                }
            })
        }
    }
})
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐