文件下载保存错误

XY道衍
发布于 2024-7-31 11:03
浏览
0收藏
    try {
      let userToken = PreferenceHelper.getValue("token").toString()
      let fileName = bean.fileName

      const documentSaveOptions = new picker.DocumentSaveOptions();
      // documentSaveOptions.newFileNames = [fileName];
      const documentViewPicker = new picker.DocumentViewPicker(this.mContext);
      documentSaveOptions.pickerMode = picker.DocumentPickerMode.DOWNLOAD;
      documentViewPicker.save(documentSaveOptions)
        .then((documentSaveResult: Array<string>) => {
          let uri = documentSaveResult[0];
          let filePath = uri + '/' + fileName
          LogUtils.info(this.TAG, `文件保存地址: ${filePath}`);
          // 判定文件是否存在
          if (fs.accessSync(filePath)) {
            // 删除文件
            fs.unlinkSync(filePath)
          }
          let config: request.DownloadConfig = {
            url: bean.downLoadUrl,
            header: {
              'Cookie': `zhgd-web-access_token=${userToken};zhgd-h5-access_token=${userToken}`
            },
            filePath: filePath
          }
          request.downloadFile(this.mContext, config).then((data: request.DownloadTask) => {
            let downloadTask: request.DownloadTask = data;
            downloadTask.on('progress', (receivedSize: number, totalSize: number) => {
              LogUtils.info(this.TAG, "download receivedSize:" + receivedSize + " totalSize:" + totalSize);
              const bean: DownloadFileResBean = new DownloadFileResBean();
              if (receivedSize !== totalSize) {
                bean.downloadedSize = receivedSize
                bean.totalSize = totalSize
                callback.onUpdate(bean)
              }
            });
            downloadTask.on('complete', async () => {
              LogUtils.info(this.TAG, `downloadFile(),complete`)
              try {
                let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
                let arrayBuffer = new ArrayBuffer(1024)
                fs.writeSync(file.fd, arrayBuffer)
                fs.closeSync(file)
                const bean: DownloadFileResBean = new DownloadFileResBean();
                bean.fileLocalPath = filePath
                callback.onSucc(bean)
              } catch (err) {
                LogUtils.info(this.TAG, `Failed to save file.err: ${JSON.stringify(err)}`);
                callback.onFail('保存失败')
              }
            });
          }).catch((err: BusinessError) => {
            LogUtils.info(this.TAG, `Failed to request the download. Code: ${err.code}, message: ${err.message}`);
            callback.onFail('下载失败')
          })
        })
        .catch((err: BusinessError) => {
          console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
          callback.onFail('下载失败')
        })
    } catch (err) {
      LogUtils.info(this.TAG, `Failed to request the download. err: ${JSON.stringify(err)}`);
      callback.onFail('下载失败')
    }
  }

返回这个错误,Invoke documentViewPicker.save failed, code is 401, message is the parameters check fails Parameter verification failed, GetInternalPath failed, filePath is not valid
请大佬指点下,这个下载文件的方法哪里有问题呢?

分类
收藏
回复
举报
回复
    相关推荐