微信小程序 不能实现真正意义的下载保存,因为微信小程序提供的存储服务是以小程序和用户维度隔离的存储服务,所以下载下来之后用户并不能访问到自己下载的文件。做不到用户下载文件之后可以快捷的找到下载的文件。只是下载到微信app的缓存里面。且微信也不建议开发人员或用户去查询本地文件存储的路径。该结论官方文档也有具体说明
https://developers.weixin.qq.com/miniprogram/dev/framework/ability/file-system.html
主要代码:
只是下载和打开

            let task = wx.downloadFile({
            url: 'https://www.gjtool.cn/pdfh5/git.pdf',
            success(res) {
                //打开下载的文件
                console.log(res, 2333);
                if (res.statusCode == 200) {
                    wx.openDocument({
                        filePath: res.tempFilePath,
                        showMenu: true,
                        success: () => {},
                        fail: () => {
                            wx.showToast({
                                title: '打开失败',
                                icon: 'none',
                                duration: 2000
                            })
                        },
                    })
                } else {
                    wx.showToast({
                        title: '下载失败',
                        icon: 'none',
                        duration: 2000
                    })
                }
            },
            fail() {
                wx.showToast({
                    title: '下载失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    task.onProgressUpdate((res) => {
        console.log('下载进度===>', res.progress)
        // this.setData({
        //   // 更新下载的进度
        //   progress: res.progress,
        // });
      })

javascript

安卓 可以下载 苹果没有保存文件
微信小程序 不能实现真正意义的下载保存,因为微信小程序提供的存储服务是以小程序和用户维度隔离的存储服务,所以下载下来之后用户并不能访问到自己下载的文件。做不到用户下载文件之后可以快捷的找到下载的文件。只是下载到微信app的缓存里面。且微信也不建议开发人员或用户去查询本地文件存储的路径。该结论官方文档也有具体说明
官方文档:
https://developers.weixin.qq.com/miniprogram/dev/framework/ability/file-system.html

安卓下载版本:

     
     let task = wx.downloadFile({
            url: 'https://www.gjtool.cn/pdfh5/git.pdf',
            success(res) {
                if (res.statusCode == 200) {
                    wx.getFileSystemManager().saveFile({
                        tempFilePath: res.tempFilePath, // 传入一个本地临时文件路径, http://tmp/开头的
                        filePath: `${wx.env.USER_DATA_PATH}/${'git.pdf'}`, //此处文件名自定义,因为tempFilePath对应的是一大长串字符
                        success(res2) {
                            console.log('保存成功', res2, '文件已保存至:' + res2.savedFilePath) // res.savedFilePath 为一个本地缓存文件路径
                            wx.openDocument({
                                filePath: res2.savedFilePath,
                                showMenu: true,
                                success: () => {},
                                fail: () => {
                                    wx.showToast({
                                        title: '打开失败',
                                        icon: 'none',
                                        duration: 2000
                                    })
                                }
                            })
                        },
                        fail(res2) {
                            console.log('保存失败', res2) // res.savedFilePath 为一个本地缓存文件路径
                        }
                    })
                } else {
                    wx.showToast({
                        title: '下载失败',
                        icon: 'none',
                        duration: 2000
                    })
                }
            },
            fail() {
                wx.showToast({
                    title: '下载失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })

    task.onProgressUpdate((res) => {
        console.log('下载进度===>', res.progress)
        // this.setData({
        //   // 更新下载的进度
        //   progress: res.progress,
        // });
      })
javascript

借鉴 https://blog.csdn.net/qq_37131884/article/details/123360114


评论
avatar
0 评论
目录