this.Context.Request.Url.Segments

本文探讨了如何通过Segments属性从绝对路径中获取URL的各部分,包括如何处理包含片段标识符和查询参数的复杂URL。

Segments属性将会返回一个以“/”为分隔的URL数组,这个URL数组同时也是来自absolute path

例如:

Absolute URI

http://www.contoso.com/Chapters/Chapter1/Sections/Section1.htm

Absolute path

/Chapters/Chapter1/Sections/Section1.htm

Segments

/Chapters/Chapter1/Sections/Section1.htm

Absolute URI

http://www.contoso.com/Chapters/Chapter1/Sections/Section1.htm#page1?answer=NO

Absolute path

/Chapters/Chapter1/Sections/Section1.htm

Segments

/Chapters/Chapter1/Sections/Section1.htm

转载于:https://www.cnblogs.com/liangyu/articles/2441869.html

import http from '@ohos.net.http'; import util from '@ohos.util'; import fs from '@ohos.file.fs'; import picker from '@ohos.file.picker'; import systemDateTime from '@ohos.systemDateTime'; import request from '@ohos.request'; import connection from '@ohos.net.connection'; import HashSet from '@ohos.util.HashSet'; import ArrayList from '@ohos.util.ArrayList'; import web_webview from '@ohos.web.webview' @Entry @Component struct Index { //要加载的网址 @State webUrl: string = "https://www.baidu.com" //要加载的文件 @State loadFileUri: string = "" //要加载的内容 @State webContent: string = `<!DOCTYPE html> <html> <body style="font-size: large;text-align: center;"> <div>测试加载文本内容!</div> </body> </html>` scroller: Scroller = new Scroller() contentScroller: Scroller = new Scroller() controller: web_webview.WebviewController = new web_webview.WebviewController() build() { Row() { Column() { Text("Web加载示例") .fontSize(14) .fontWeight(FontWeight.Bold) .width('100%') .textAlign(TextAlign.Center) .padding(10) Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) { Text("网址:") .fontSize(14) .width(50) .flexGrow(0) TextInput({ text: this.webUrl }) .onChange((value) => { this.webUrl = value }) .width(110) .fontSize(11) .flexGrow(1) Button("加载") .onClick(() => { this.controller.loadUrl(this.webUrl); }) .width(60) .fontSize(14) .flexGrow(0) } .width('100%') .padding(5) Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) { Text("文件:") .fontSize(14) .width(50) .flexGrow(0) TextInput({ text: this.loadFileUri }) .onChange((value) => { this.loadFileUri = value }) .width(110) .fontSize(11) .flexGrow(1) Button("选择") .onClick(() => { this.selectFile() }) .width(60) .fontSize(14) .flexGrow(0) Button("加载") .onClick(() => { this.loadLocalFile() }) .width(60) .fontSize(14) .flexGrow(0) } .width('100%') .padding(5) Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { Text("下方输入框的文本内容:") .fontSize(14) .width(200) .flexGrow(0) Button("加载") .onClick(() => { this.controller.loadData(this.webContent, "text/html", "UTF-8") }) .width(60) .fontSize(14) .flexGrow(0) } .width('100%') .padding(5) Scroll(this.contentScroller) { TextArea({ text: this.webContent }) .onChange((value) => { this.webContent = value }) .backgroundColor(0xffffee) .width('100%') .fontSize(11) } .align(Alignment.Top) .backgroundColor(0xeeeeee) .height(120) .scrollable(ScrollDirection.Vertical) .scrollBar(BarState.On) .scrollBarWidth(20) Scroll(this.scroller) { Web({ src: $rawfile("index.html"), controller: this.controller }) .padding(10) .width('100%') .backgroundColor(0xeeeeee) } .align(Alignment.Top) .backgroundColor(0xeeeeee) .height(300) .flexGrow(1) .scrollable(ScrollDirection.Vertical) .scrollBar(BarState.On) .scrollBarWidth(20) } .width('100%') .justifyContent(FlexAlign.Start) .height('100%') } .height('100%') } //选择文件,为简单起见,选择一个不太大的网页文件 selectFile() { let documentPicker = new picker.DocumentViewPicker(); documentPicker.select().then((result) => { if (result.length > 0) { this.loadFileUri = result[0] } }).catch((err) => { console.error(err.message) }); } //加载本地文件 async loadLocalFile() { let context = getContext(this) let segments = this.loadFileUri.split('/') //文件名称 let fileName = segments[segments.length-1] + ".html" //计划复制到的目标路径 let realUri = context.cacheDir + "/" + fileName //复制选择的文件到沙箱cache文件夹 try { let file = await fs.open(this.loadFileUri); fs.copyFileSync(file.fd, realUri) this.controller.loadUrl(`file://${realUri}`); } catch (err) { console.error(err.message) } } }这是我的代码,问题在哪,这是鸿蒙语言
最新发布
05-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值