所以我想使用jquery ajax将视频从HTML5发送到服务器端(节点js),但我不确定是否最好的方法。它应该像这样工作: 客户端上传视频->节点接收视频->节点js调用python脚本处理视频并返回视频->节点js将视频发送给客户端。 这是我写的一些代码。
客户端
<!DOCTYPE html>
<html lang="en">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
function sendData(){
var data = new FormData();
$.each($('#upload-input')[0].files, function(i, file) {
data.append('file-'+i, file);
});
$.ajax({
url: 'http://localhost:3000/newFlavour',
data: data,
cache: false,
contentType: false,
processData: false,
method: 'POST',
type: 'POST', // For jQuery < 1.9
success: [function(data){
receiveData(data);
}]
});}
function receiveData(response){
response = JSON.parse(response);
console.log(response)
}
</script>
<head>
<meta charset="UTF-8">
<title>Serviços disponibilizados</title>
</head>
<body>
<input type="file" accept="*/video/ogg" accept="*/video/mp4" id="upload-input">
<button onclick="sendData()"></button>
</body>
</html>
服务器端
formidable = require('formidable');
app = require ('express')();
const http = require('http').Server(app);
app.post('/newFlavour', function (req, res){
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.send(files)
res.status(200)
});
form.on('fileBegin', function (name, file){
file.path = 'C:/Users/veryan/Desktop/teste/' + file.name;
});
form.on('file', function (name, file){
console.log('Uploaded ' + file.name);
});
});
http.listen(3000, function(){
console.log('Listening on port 3000 ...');
});