HarmonyOS 鸿蒙Next 如何拦截视频链接,读取本地视频返回给web
HarmonyOS 鸿蒙Next 如何拦截视频链接,读取本地视频返回给web
网络请求我拦截下来了, 可是视频读取后,怎么返回给前端页面呢?
const begin = parseInt(subStrArray[0])
const end = (subStrArray[1] && subStrArray[1].length > 0) ? parseInt(subStrArray[1]) : 0
const file = fileIo.openSync(filePath, fileIo.OpenMode.READ_ONLY)
const fileStat = fileIo.statSync(file.fd)
const size = fileStat.size
let contentLength = (size > end) ? (end-begin+1) : (size-begin+1);
if (end === 0) {
contentLength = size - begin
}
if (contentLength < 0) {
return undefined
}
let buf = new ArrayBuffer(contentLength);
let readOption: ReadOptions = {
offset: begin,
length: contentLength
};
const readLength = fileIo.readSync(file.fd, buf, readOption)
const responseWeb: WebResourceResponse = new WebResourceResponse();
let headers: Array<Header> = new Array<Header>()
headers.push({ headerKey:"Content-Type", headerValue:mimeType })
headers.push({ headerKey:"Content-Length", headerValue:readLength.toString() })
headers.push({ headerKey:"Content-Range", headerValue:`bytes ${begin}-${begin+contentLength}/${size}` })
headers.push({ headerKey:"Accept-Ranges", headerValue: 'bytes' })
responseWeb.setResponseHeader(headers)
responseWeb.setResponseMimeType(mimeType)
responseWeb.setResponseData(buf)
if (size <= begin + readLength) {
responseWeb.setResponseCode(200)
} else {
responseWeb.setResponseCode(206)
}
需要指定responseWeb.setResponseEncoding()吗? 我读取的是二进制,setResponseEncoding设置什么值呢?
更多关于HarmonyOS 鸿蒙Next 如何拦截视频链接,读取本地视频返回给web的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复