HarmonyOS 调用fs.openSync接口时提醒权限问题

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

保存文件到文件管理需要在通过DocumentViewPicker,可参考如下demo

import web_webview from '@ohos.web.webview'
import business_error, { BusinessError } from '@ohos.base'
import { picker } from '@kit.CoreFileKit';
import fs from '@ohos.file.fs';

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController();
  delegate: web_webview.WebDownloadDelegate = new web_webview.WebDownloadDelegate();

  build() {
    Column() {
      Button('setDownloadDelegate')
        .onClick(() => {
          try {
            this.delegate.onBeforeDownload((webDownloadItem: web_webview.WebDownloadItem) => {
              console.log("======will start a download." + webDownloadItem.getSuggestedFileName());
              webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName());
            })
            this.delegate.onDownloadUpdated((webDownloadItem: web_webview.WebDownloadItem) => {
              console.log("======download update guid: " + webDownloadItem.getGuid());
            })
            this.delegate.onDownloadFailed((webDownloadItem: web_webview.WebDownloadItem) => {
              console.log("download failed guid: " + webDownloadItem.getGuid());
            })
            this.delegate.onDownloadFinish((webDownloadItem: web_webview.WebDownloadItem) => {
              console.log("======download finish guid: " + webDownloadItem.getGuid());
              console.log("======downloadFullPath:" + webDownloadItem.getFullPath())

              let documentSaveOptions = new picker.DocumentSaveOptions();
              documentSaveOptions.newFileNames = [webDownloadItem.getSuggestedFileName()];
              let documentPicker = new picker.DocumentViewPicker();
              documentPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
                let uri = documentSaveResult[0];
                let path = "/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()
                let sanFile = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
                let pubFile = fs.openSync(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
                fs.copyFileSync(sanFile.fd, pubFile.fd)
                console.info('DocumentViewPicker.save successfully, DocumentSaveResult uri: ' +
                JSON.stringify(documentSaveResult));
              }).catch((err: BusinessError) => {
                console.error('DocumentViewPicker.save failed with err: ' + JSON.stringify(err));
              });

            })
            this.controller.setDownloadDelegate(this.delegate);
          } catch (error) {
            let e: business_error.BusinessError = error as business_error.BusinessError;
            console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
          }
        })
      Button('startDownload')
        .onClick(() => {
          try {
            this.controller.startDownload('https:myURL.pdf');
          } catch (error) {
            let e: business_error.BusinessError = error as business_error.BusinessError;
            console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
          }
        })
      Web({ src: 'www.huawei.com', controller: this.controller })
    }.margin({ top: 200 })
  }
}

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS拍照后调用openSync方法报错
592浏览 • 1回复 待解决
HarmonyOS 关于代理提醒数量的问题
423浏览 • 1回复 待解决
HarmonyOS fs模块读取文件的问题
547浏览 • 1回复 待解决
fs.unlink接口无法删除文件夹
1861浏览 • 1回复 待解决
如何解决定位接口调用问题
324浏览 • 1回复 待解决
HarmonyOS 关于代理提醒的设置问题
478浏览 • 1回复 待解决
HarmonyOS关于使用fs.copyfile的问题
509浏览 • 1回复 待解决
HarmonyOS 权限配置问题
352浏览 • 1回复 待解决
HarmonyOS定位权限问题
428浏览 • 1回复 待解决
HarmonyOS如何调用http/https接口?
10368浏览 • 3回复 已解决
HarmonyOS相册权限问题
598浏览 • 1回复 待解决
HarmonyOS 相机权限问题
477浏览 • 1回复 待解决
HarmonyOS 手机震动接口调用无效果
302浏览 • 1回复 待解决
HarmonyOS 相机权限问题咨询
26浏览 • 1回复 待解决