以下是我传输视频的代码(我使用Sails.js)
// ./api/controllers/AttachmentController.js
module.exports = {
stream: (req, res) => {
"use strict";
let attachmentId = req.params.id;
Attachment.findOne(parseInt(attachmentId))
.then( file => {
let stat = fs.statSync(file.path);
let total = stat.size;
if (req.headers['range']) {
let range = req.headers.range;
let parts = range.replace(/bytes=/, "").split("-");
let partialstart = parts[0];
let partialend = parts[1];
let start = parseInt(partialstart, 10);
let end = partialend ? parseInt(partialend, 10) : total-1;
let chunksize = (end-start)+1;
let stream = fs.createReadStream(file.path, {start: start, end: end});
res.writeHead(206, { 'Content-Range': 'bytes ' + start + '-' + end + '/' + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': file.mimetype });
stream.pipe(res);
} else {
res.writeHead(200, { 'Content-Length': total, 'Content-Type': file.mimetype });
fs.createReadStream(file.path).pipe(res);
}
})
.catch(err => {
sails.log.error(err);
return res.serverError();
})
}
}
在视图中
<div class="embed-responsive embed-responsive-16by9">
<video class="embed-responsive-item center-block" controls="controls">
<source src="/attachment/stream/332" type="video/mp4" />{{ __('Video not supported') }}
</video>
</div>
当我使用http访问我的视频时,它适用于所有浏览器。当它是https时,它适用于除Chrome之外的所有浏览器。
编辑:在寻找Chrome版本时,它更新为52.0.2743.82m,现在可以使用。