收藏
回答

DownloadTask.onProgressUpdate 安卓真机调试无法监听下载进度变化事件

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug DownloadTask.onProgressUpdate 微信安卓客户端 8.0.30 2.28.1 [870]

手机型号:小米10s Xiaomi-M2102J2SC M2102J2SCarm64-v8a

pre_show: function (src, name, callback = false{
  // console.log(src);
  // console.log(name);
  // console.log(callback);
  if (!(src && name)) {
    return false;
  }
  wx.showLoading({
    title"下载中...",
    masktrue,
  });
  var fs = wx.getFileSystemManager();
  var df = wx.downloadFile({
    url: src,
    // filePath: `${wx.env.USER_DATA_PATH}/${name}`,
    successfunction (params{
      // console.log(params);
      if (params.statusCode === 200) {
        // 重新改写文件后缀
        fs.saveFile({
          tempFilePath: params.tempFilePath,
          // wx.env.USER_DATA_PATH 是微信提供了一个用户文件目录给开发者,开发者对这个目录有完全自由的读写权限
          filePath`${wx.env.USER_DATA_PATH}/${name}`,
          successfunction (params2{
            // console.log(params2);
            // console.log(params2.savedFilePath) // params2.savedFilePath 为一个本地缓存文件路径
            var src_arr = src.split(".");
            var ext = src_arr[src_arr.length - 1].toLowerCase();
            // console.log(ext);
            if (callback != false) {
              callback();
            }
            if (["gif""jpg""jpeg""png""bmp"].indexOf(ext) !== -1) {
              if (callback != false) {
                // 保存图片
                wx.saveImageToPhotosAlbum({
                  filePath: params2.savedFilePath,
                  completefunction (params4{
                    // console.log(params4);
                    wx.hideLoading();
                    df.offProgressUpdate();
                  },
                  successfunction (params4{
                    wx.showToast({
                      title"图片已保存",
                      masktrue,
                    });
                  },
                  failfunction (params4{
                    wx.showToast({
                      title"图片保存失败",
                      masktrue,
                      icon"none",
                    });
                  },
                });
              } else {
                wx.previewImage({
                  urls: [src],
                  completefunction (params4{
                    // console.log(params4);
                    wx.hideLoading();
                    df.offProgressUpdate();
                  },
                })
              }
            } else {
              wx.openDocument({
                filePath: params2.savedFilePath,
                showMenutrue,
                completefunction (params4{
                  // console.log(params4);
                  wx.hideLoading();
                  df.offProgressUpdate();
                },
                successfunction (params4{
                  wx.showToast({
                    title"文件已打开",
                    masktrue,
                  });
                },
                failfunction (params4{
                  wx.showToast({
                    title"文件打开失败",
                    masktrue,
                    icon"none",
                  });
                },
              })
            }


          },
        });
      }


    },
  });


  df.onProgressUpdate(function (params{
    // console.log(params);
    // wx.showLoading({
    //   title: "下载中..." + params.progress + "%",
    //   // title: JSON.stringify(params),
    //   mask: true,
    // });
    wx.showToast({
      titleJSON.stringify(params),
      masktrue,
      icon"none",
    });
  });


},


回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容