微信小程序文件下载

1.下载文档

如下就可以下载成功了,但是,这就又发现了一个问题,下载好的文档不知道去哪里查找。

uni.downloadFile({

    url: fileUrl,

    success (res) {

        uni.saveFile({

            tempFilePath: res.tempFilePath,

            success (res) {

                Toast('下载成功')

            },

            fail(error){

                Toast('下载失败')

            }

        })

    }

})

下面我们就换一种方式,使用  uni.openDocument() 直接将下载好的文件打开,就能找到位置了:

async handleLongpress() {

        var fileType = this.format;

        uni.downloadFile({

          url: this.docUuidUrl,

          success: function(res) {

            var filePath = res.tempFilePath;

            uni.openDocument({

              filePath: filePath,

              fileType: fileType,

              success: function(res) {

                console.log('打开文档成功');

              },

            });

          }

        });

      },

文件类型记得要传。

2.下载视频

async handleLongpress() {

        await uni.showLoading({

          title: '下载中'

        })

        // 1. 将远程文件 下载到小程序内存中

        const {

          tempFilePath

        } = (await uni.downloadFile({

          url: this.vedioUuidUrl

        }))[1]

        // 2. 将内存中的文件 下载到本地系统相册

        uni.saveVideoToPhotosAlbum({

          filePath: tempFilePath

        })

        uni.hideLoading()

        // 3. 提示用户下载成功

        await uni.showToast({

          title: '下载成功',

          icon: 'none'

        })

      },

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容