视频大文件的块数据传输
大文件例如视频的读取,不能像单张图片那样,等到完全读取完毕,才把数据给到前端。此时,就需要部分、部分的返回数据。
本文将尽可能详细的介绍,为了实现这个需求,前后端的都应该怎么搞
后端配置
var fs = require('fs'); function readBigFileEntry(filename, response) { path.exists(filename, function(exists) { if (!filename || !exists) { response.writeHead(404); response.end(); return; } var readStream = fs.ReadStream(filename); var contentType = 'none'; var ext = path.extname(filename); switch (ext) { case ".flv": contentType = "video/flv"; break; } response.writeHead(200, { 'Content-Type' : contentType, 'Accept-Ranges' : 'bytes', 'Server' : 'Microsoft-IIS/7.5', 'X-Powered-By' : 'ASP.NET' }); readStream.on('close', function() { response.end(); console.log("Stream finished."); }); readStream.pipe(response); }); }前端部分
…待补充
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
