微信小程序-wx.downloadFile()

在小程序端,.js文件中,使用以下代码下载文件

wx.showLoading({
  title: '下载中',
});
console.log("下载文件:",fileId);
wx.cloud.downloadFile({
  fileID: fileId,
  timeout: 30,
  success: (result) => {
    console.log("下载成功",result.tempFilePath);
wx.downloadFile({
      url: "https://XXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pdf",  //源文件地址
      filePath:wx.env.USER_DATA_PATH+'/{要更改的名称}.pdf',  //指定的本地文件路径   一定要带文件后缀名  不然无法打开   wx.env.USER_DATA_PATH类似本地文件根目录
      success(res) {
        console.log(res)
        let data = res.filePath;
        wx.openDocument({
          filePath: data,
          showMenu: true  //表示右上角是否有转发按钮
        })
      }
    })
    //已验证Android,iOS 都可更改
    //使用FileSystemManager.rename()  模拟器 有权限进行写入等操作    Android,iOS 都无权限(已验证)   已放弃

  },
  fail: (res) => {
    console.log("下载失败:",res);
    wx.showToast({
      title: res.errMsg,
      icon:'error'
    });
  },
  complete: (res) => {
    wx.hideLoading();
  },
})

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过请求网页获取信息并显示,你可以使用微信小程序提供的网络请求 API wx.request()。具体步骤如下: 1. 在小程序的 app.json 文件中配置要请求的网页域名,如: ``` "networkTimeout": { "request": 10000, "connectSocket": 10000, "uploadFile": 10000, "downloadFile": 10000 }, "debug": true, "networkTimeout": { "request": 10000, "connectSocket": 10000, "uploadFile": 10000, "downloadFile": 10000 }, "appid": "wx1234567890", "pages": [ "pages/index/index" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "小程序", "navigationBarTextStyle": "black" }, "tabBar": { "list": [ { "pagePath": "pages/index/index", "text": "首页" }, { "pagePath": "pages/about/about", "text": "关于" } ] }, "networkTimeout": { "request": 10000, "connectSocket": 10000, "uploadFile": 10000, "downloadFile": 10000 }, "permission": { "scope.userLocation": { "desc": "小程序需要获取你的地理位置" } }, "window": { "navigationBarBackgroundColor": "#ffffff", "navigationBarTextStyle": "black", "navigationBarTitleText": "小程序", "backgroundColor": "#eeeeee", "backgroundTextStyle": "light" }, "sitemapLocation": "sitemap.json", "requiredBackgroundModes": ["audio"] ``` 2. 在小程序的 js 文件中使用 wx.request() 方法请求网页,并获取数据,如: ``` wx.request({ url: 'https://www.example.com/data.json', success: function(res) { console.log(res.data) } }) ``` 3. 将获取到的数据显示在小程序的界面上,如: ``` Page({ data: { info: '' }, onLoad: function() { var that = this wx.request({ url: 'https://www.example.com/data.json', success: function(res) { that.setData({ info: res.data }) } }) } }) ``` 其中,info 是一个数据绑定变量,用来保存从网页获取的数据。在 onLoad 方法中,使用 that.setData() 方法将获取到的数据更新到界面上。在小程序wxml 文件中,可以使用 {{info}} 引用该数据绑定变量,将数据显示在界面上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值