中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
import { http } from '@kit.NetworkKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { common } from '@kit.AbilityKit'; import fs, { ReadOptions, WriteOptions } from '@ohos.file.fs'; import { filePreview } from '@kit.PreviewKit'; import { pdfService } from '@kit.PDFKit'; import web_webview from '@ohos.web.webview' import { promptAction } from '@kit.ArkUI'; @Entry @Component struct Index { context = getContext(this) as common.UIAbilityContext; controller: web_webview.WebviewController = new web_webview.WebviewController(); aboutToAppear(): void { } build() { Column() { Button("下载pdf").onClick(() => { // 每一个httpRequest对应一个HTTP请求任务,不可复用 let httpRequest = http.createHttp(); httpRequest.requestInStream('xxx.pdf', { extraData: http.HttpDataType.ARRAY_BUFFER }) // 用于订阅HTTP流式响应数据接收事件 let res = new ArrayBuffer(0); httpRequest.on('dataReceive', (data: ArrayBuffer) => { const newRes = new ArrayBuffer(res.byteLength + data.byteLength); const resView = new Uint8Array(newRes); resView.set(new Uint8Array(res)); resView.set(new Uint8Array(data), res.byteLength); res = newRes; console.info('res length: ' + res.byteLength); }); // 用于订阅HTTP流式响应数据接收完毕事件 httpRequest.on('dataEnd', () => { console.info('No more data in response, data receive end'); promptAction.showToast({ message: "下载成功" }) let srcFile = fs.openSync(this.context.filesDir + '/test.pdf', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); fs.write(srcFile.fd, res) }); }) Button("加载pdf").onClick(() => { const filePath = `${this.context.filesDir}/test.pdf` let document: pdfService.PdfDocument = new pdfService.PdfDocument(); document.loadDocument(filePath, '', (progress: number) => { return progress; }); this.controller.loadUrl(`file://${this.context.filesDir}/test.pdf#scrollbars=0&toolbar=0&statusbar=0`) }) Web({ src: `www.huawei.com`, controller: this.controller }).domStorageAccess(true) } } }