如何使用Web组件下载pdf文件并展示给用户

如何使用Web组件下载pdf文件并展示给用户

HarmonyOS
2024-06-11 23:40:02
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
yu_qingbo

参考如下,需要在module.json5配置如下权限。

"requestPermissions": [ 
  { 
    "name": "ohos.permission.INTERNET" 
  }, 
  { 
    "name": "ohos.permission.WRITE_MEDIA" 
  }, 
  { 
    "name": "ohos.permission.READ_NEDIA" 
  } 
]
import common from ‘@ohos.app.ability.common’; 
import request from ‘@ohos.request’; 
import { BusinessError } from ‘@ohos.base’; 
 
// 获取应用文件路径 
let context = getContext(this) as common.UIAbilityContext; 
let filesDir = context.filesDir; 
 
@Entry 
@Component 
export struct Index1 { 
  @State message: string = ‘Hello World’; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
        Button(“下载”) 
        .onClick(() => { 
          try { 
            console.log(‘downloadTask1 filesDir’ + filesDir + ‘/00sg00izg2.jpg’); 
            //下载文件 
            request.downloadFile(context, { 
              url: ‘https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2023%2F1108%2Fea454272j00s3rxox001tc000sg00izg.jpg&thumbnail=660x2147483647&quality=80&type=jpg’, 
              filePath: filesDir + ‘/00sg00izg2.jpg’ 
            }).then((downloadTask: request.DownloadTask) => { 
              //开启回调 
              downloadTask.on(‘complete’, () => { 
                console.info(‘downloadTask1 complete’); 
              }) 
            }).catch((err: BusinessError) => { 
              console.error(Invoke downloadTask failed, code is ${err.code}, message is ${err.message}); 
            }); 
          } catch (error) { 
            let err: BusinessError = error as BusinessError; 
            console.error(Invoke downloadTask downloadFile failed, code is ${err.code}, message is ${err.message}); 
          } 
        }) 
 
          .width(‘100%’) 
      } 
      .height(‘100%’) 
    } 
  }}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
分享
微博
QQ
微信
回复
2024-06-12 23:53:18


相关问题
HarmonyOS web组件预览PDF文件问题
598浏览 • 1回复 待解决
HarmonyOS使用Web组件预览PDF和图片
1298浏览 • 1回复 待解决
使用Web组件下载能力Demo
968浏览 • 1回复 待解决
应用内组件截图保存到用户文件
2016浏览 • 1回复 待解决
如何通过web组件预览PDF
1142浏览 • 1回复 待解决
使用web组件实现预览沙箱中pdf
2627浏览 • 1回复 待解决
HarmonyOS 使用PDF kit预览PDF文件
495浏览 • 1回复 待解决
HarmonyOS web组件加载pdf问题
1211浏览 • 1回复 待解决
HarmonyOS Web组件加载pdf预览
906浏览 • 1回复 待解决