gpt4 book ai didi

javascript - 如何使用 fs.readStream 和 fs.writesream 发送和接收视频(.mp4)文件[从客户端到服务器,反之亦然][在 Node Js 中]?

转载 作者:太空宇宙 更新时间:2023-11-04 01:28:21 24 4
gpt4 key购买 nike

   ## Here is my Unsucessfull approach  ##


----------


> Server side code
> Recieve Video stream from client and save it in .mp4 format

var express = require('express');
var app = global.app = exports.app = express();
var fs = require('fs');

app.post('/video_main', (req, res, next) => {
console.log(req)
const video_stream = fs.createWriteStream('vde.mp4')
req.pipe(video_stream)
//save the video stream to vde.mp4
})


app.use('*', function(req, res, next) {

return res.status(404).json({
'status': 'FAILURE',
'message': 'Not found'
})

})

app.listen('3000');
// server listening on http://localhost:3000/


----------


> Client side code
> send video stream to server

const request = require('request')
const fs = require('fs')
var readablestream = fs.createReadStream('SampleVideo_1280x720_1mb.mp4')
const write = fs.createWriteStream('test.mp4');
const r = request.post("http://localhost:3000/video_main");
r.pipe(readablestream);

/*

*/

最佳答案

代码的主要错误是在客户端,最后一行应该是

readablestream.pipe(r);

因为您正在将数据从可读流传输到请求中。

为了将来的引用,您应该始终遵循约定:

readstream.pipe(writestream);

I figured this out by analyzing the code on this github, which has pretty nice implementation of file transfer over html:

https://gist.github.com/alepez/9205394

关于javascript - 如何使用 fs.readStream 和 fs.writesream 发送和接收视频(.mp4)文件[从客户端到服务器,反之亦然][在 Node Js 中]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56821139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com