gridfs - 使用 react-native-video 显示视频,使用 GridFSBucket 来自 express
问题描述
我在这里使用 react-native-video 显示视频时遇到了困难。服务器使用 Express,并使用 GridFSBucket 从 mongodb 检索视频。
问题是:来自 GridFSBucket 的视频不会显示。但是当我尝试将视频放在公用文件夹中时,它会显示。
所以,我的猜测是我提供视频的方式有问题。这是我来自服务器的代码:
const bucket = new GridFSBucket(db.original(), { bucketName: "fs" });
const fileDetail = await db.original().collection("fs.files").findOne({_id: idObj});
if (isNullOrUndefined(fileDetail)) {
throw new NoContentException(`asset not found for id ${id}`);
}
const range = req.headers["range"]
if (range && typeof range === "string") {
const parts = range.replace(/bytes=/, "").split("-");
const partialstart = parts[0];
const partialend = parts[1];
const start = parseInt(partialstart, 10);
const end = partialend ? parseInt(partialend, 10) : fileDetail.length - 1;
const chunksize = (end - start) + 1;
res.writeHead(206, {
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Range': 'bytes ' + start + '-' + end + '/' + fileDetail.length,
'Content-Type': fileDetail.contentType
});
bucket.openDownloadStream(idObj, {start, end}).pipe(res);
} else {
res.header('Content-Length', fileDetail.length);
res.header('Content-Type', fileDetail.contentType);
bucket.openDownloadStream(idObj).pipe(res);
}
提前感谢您的回答^_^
解决方案
我找到了解决方案。
该行:
bucket.openDownloadStream(idObj, {start, end}).pipe(res);
应该:
bucket.openDownloadStream(idObj, {start, end: end - 1}).pipe(res);
推荐阅读
- javascript - 对具有返回值的函数反应 useMemo
- json - 格式化 Excel (xlsx) 以使用数组转换为 JSON
- google-apps-script - 如何在 onTriggerFunction 函数中访问 Gmail 邮件元数据?
- android - 如何让 AppBarLayout 像 Gmail 一样有透明背景?
- git - Github 试图避免每次都被要求提供凭据
- google-sheets - Google表格CountIf:其他单元格中的文本
- java - 为什么spring应用程序在使用docker compose运行时无法连接到couchbase
- ruby-on-rails - Ruby on Rails:添加另一个 Or 子句
- javascript - 输入时如何制作这样的json文件
- airflow - 气流调度每月作业