HarmonyOS 文件下载后文件夹内找不到对应文件?

使用@ohos.request进行文件下载,但是在系统文件夹中找不到对应的文件,网络资源文件是不是仅支持下载至应用文件目录呢?如果是的话应用的文件目录在哪里呢?

阅读 428
1 个回答

文件下载,下载的是应用的沙箱目录,沙箱目录参考文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/app-sandbox-directory-V5\#%E5%BA%94%E7%94%A8%E6%B2%99%E7%AE%B1%E7%9B%AE%E5%BD%95%E4%B8%8E%E5%BA%94%E7%94%A8%E6%B2%99%E7%AE%B1%E8%B7%AF%E5%BE%84

IDE右下角Device File Browser可以查看沙箱目录下的文件

文件下载到手机系统文件目录内请参考:

import { common } from '@kit.AbilityKit';
import { BusinessError, request } from '@kit.BasicServicesKit';
import fs from '@ohos.file.fs';
import { buffer } from '@kit.ArkTS';
import { picker, storageStatistics } from '@kit.CoreFileKit';
import { webview } from '@kit.ArkWeb';
let context = getContext(this) as common.UIAbilityContext;
@Entry
@Component
struct testPdfSave {
  testIndex: number = 0;
  build() {
    Column() {
      Button('保存').onClick(() => {
        let filePath = context.cacheDir + "/test" + this.testIndex + ".pdf";
        this.testIndex = this.testIndex + 1
        let url = "https://xxx"
        try {
          request.downloadFile(context, {
            url: url,
            filePath: filePath
          }).then((data: request.DownloadTask) => {
            let downloadTask: request.DownloadTask = data;

            downloadTask.on('complete', () => {
              console.info('download complete');
              let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
              let arrayBuffer = new ArrayBuffer(4096000);
              let readLen = fs.readSync(file.fd, arrayBuffer);
              let buf = buffer.from(arrayBuffer, 0, readLen);
              console.info(`content of File: ${buf.toString()}`);
              let writeLen = fs.writeSync(file.fd, arrayBuffer);
              fs.closeSync(file);

              const documentSaveOptions = new picker.DocumentSaveOptions(); // 创建文件管理器选项实例
              documentSaveOptions.newFileNames = ["1.pdf"]; // 保存文件名(可选)
              documentSaveOptions.fileSuffixChoices = ['']; // 保存文件类型(可选)
              let uris: Array<string> = [];
              const documentViewPicker = new picker.DocumentViewPicker(); // 创建文件选择器实例
              documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
                uris = documentSaveResult;
                console.info('documentViewPicker.save to file succeed and uris are:' + uris);
                let uri = uris[0];
                let file2 = fs.openSync(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
                let writeLen2 = fs.writeSync(file2.fd, arrayBuffer);
                fs.closeSync(file2);
              }).catch((err: BusinessError) => {
                console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
              })
            })

            let failCallback = () => {
              console.info('Download task fail.');
            };
            downloadTask.on('fail', failCallback);

          }).catch((err: BusinessError) => {
            console.error(`downLoadFileTask failed, code is ${err.code}, message is ${err.message}`);
          });
        } catch (error) {
          console.error(`downLoadFileTask failed, code is ${error.code}, message is ${error.message}`);
        }

      })
    }
    .height('100%')
    .width('100%')
  }
}
logo
HarmonyOS
子站问答
访问
宣传栏