#鸿蒙通关秘籍#鸿蒙系统中如何利用DOWNLOAD模式保存文件至特定目录?

HarmonyOS
4天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
网络小高手

在鸿蒙系统内使用FilePicker时,可以选择将pickerMode配置为DOWNLOAD模式,将文件保存在公共路径的download目录下:

javascript import { picker } from '@kit.CoreFileKit'; import { fileIo as fs } from '@kit.CoreFileKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { common } from '@kit.AbilityKit';

创建文件保存选项实例并设置保存模式:

javascript const documentSaveOptions = new picker.DocumentSaveOptions(); documentSaveOptions.pickerMode = picker.DocumentPickerMode.DOWNLOAD;

let uri = ''; let context = getContext(this) as common.Context; const documentViewPicker = new picker.DocumentViewPicker(context);

documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => { uri = documentSaveResult[0]; console.info('documentViewPicker.save succeed and uri is:' + uri); }).catch((err: BusinessError) => { console.error(Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}); });

DOWNLOAD模式下,返回的URI直接指向创建的应用专属目录。


分享
微博
QQ
微信
回复
4天前
相关问题