2 回复
import { request } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { picker } from '@kit.CoreFileKit';
import { fileIo as fs } from '@kit.CoreFileKit';
import { common } from '@kit.AbilityKit';
import { JSON } from '@kit.ArkTS';
@Component
export struct FileDemo {
@State pdfUri: string = ''
@State downLoadFileSavePath: string = ''
requestPdf() {
let context = getContext(this) as common.UIAbilityContext;
let fileName = Date.now().toString() + '.pdf'
let filePath = context.filesDir + `/${fileName}`;
try {
request.downloadFile(getContext(), {
url: 'https://xxx/xxx.pdf',
filePath: filePath
}).then((data: request.DownloadTask) => {
let downloadTask: request.DownloadTask = data;
downloadTask.on('complete', () => {
let documentSaveOptions = new picker.DocumentSaveOptions();
documentSaveOptions.newFileNames = [fileName];
let documentViewPicker = new picker.DocumentViewPicker();
documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
try {
this.downLoadFileSavePath = documentSaveResult[0];
let sanFile = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
let pubFile = fs.openSync(this.downLoadFileSavePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
fs.copyFileSync(sanFile.fd, pubFile.fd)
fs.closeSync(sanFile.fd)
fs.closeSync(pubFile.fd)
} catch (e) {
console.log(`Failed to copy file ${JSON.stringify(e)}`)
}
})
})
}).catch((err: BusinessError) => {
console.error(`Failed to request the download. Code: ${err.code}, message: ${err.message}`);
})
} catch (err) {
console.error(`Failed to request the download. err: ${JSON.stringify(err)}`);
}
}
build() {
Column() {
Button('request PDF')
.onClick(() => {
this.requestPdf()
})
}
.width('100%')
.height('100%')
}
}
更多关于HarmonyOS 鸿蒙Next 下载并保存DPF到本地的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS(鸿蒙)系统中,下载并保存DPF(Dynamic Program File,动态程序文件,这里假设为一种鸿蒙特有的文件格式)到本地,可以通过以下步骤实现:
-
获取下载链接:首先,确保你有一个有效的DPF文件的下载链接。这个链接可以从合法的应用分发平台或开发者网站获取。
-
使用内置浏览器:打开鸿蒙系统的内置浏览器,粘贴并访问上述下载链接。
-
开始下载:在浏览器中,点击“下载”按钮,系统会提示你选择下载路径。
-
选择保存位置:在弹出的文件选择器中,浏览并选择你希望保存DPF文件的本地路径。
-
完成下载:点击“确定”或“保存”按钮,浏览器将开始下载文件,并在完成后自动保存到指定的本地位置。
-
验证文件:下载完成后,前往你选择的保存位置,检查是否成功保存了DPF文件。
注意:
- 确保你的鸿蒙系统版本支持DPF文件的下载和保存。
- 下载过程中,请确保网络连接稳定,以避免下载失败。
- 如果遇到权限问题,请检查你的应用权限设置,确保浏览器有写入存储的权限。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html