文件下载到手机

loadVideoWithUrl(url: string) {

  this.dialogId=DialogHelper.showLoadingDialog()

  // 使用request下载图片

  http.createHttp().request(url,

    {

      method: http.RequestMethod.GET,

      connectTimeout: 60000,

      readTimeout: 60000,

      expectDataType: 2,

    },

    async (error: BusinessError, data: http.HttpResponse) => {

      DialogHelper.closeDialog(this.dialogId)

      if (error) {

        console.error(`http reqeust failed with. Code: ${error.code}, message: ${error.message}`);

      } else {

        if (HttpConstants.SUCCESS_CODE === data.responseCode) {

          let videoBuffer: ArrayBuffer = data.result as ArrayBuffer;

          try {

            //

            let uris = url.split('/')//-------http://192.168.1.243/2025/03/05/b037b1a863e9444589b21a0482d3f1e0.pdf

            let atten=uris[uris.length-1].split('.')[uris[uris.length-1].split('.').length-1]//文件后缀名

            let name=uris[uris.length-1].split('.')[0]//文件名 不带后缀民

            let uri: string = '';

            // 请确保 getContext(this) 返回结果为UIAbilityContext

            let context = getContext(this) as common.Context;

            const documentViewPicker = new picker.DocumentViewPicker(context);

            const documentSaveOptions = new picker.DocumentSaveOptions();

            documentSaveOptions.pickerMode = picker.DocumentPickerMode.DOWNLOAD;

            documentViewPicker.save(documentSaveOptions ).then((documentSaveResult: Array<string>) => {

              uri = documentSaveResult[0];

              console.info('documentViewPicker.save succeed and uri is:' + uri);

              const testFilePath = new fileUri.FileUri(uri + '/【虎翅云】'+(name)+'-附件.'+atten).path;

              const file = fs.openSync(testFilePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);

              fs.writeSync(file.fd, videoBuffer);

              fs.closeSync(file.fd);

              ToastUtil.showToast('已保存至手机下载目录下');  

            })

          } catch (error) {

            console.error("error is " + JSON.stringify(error))

          }

        } else {

          console.error("error occurred when image downloaded!")

        }

}

    })

}

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

推荐阅读更多精彩内容