我已经附加了客户端和服务器端代码。 我正在使用套接字上载文件。它对视频文件以外的所有文件均适用。 我正在分块发送数据,因此大小不会成为问题。我在做什么错了?
客户
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Echo server</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<h1>File Upload</h1>
<form id="form" id="chat_form">
<input type="file" id="file_input" />
<input type="button" onclick="submit1()" value="submit">
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script type="text/javascript">
var socket = io('http://localhost:8080');
var fReader;
socket.on('echo', function (data) {
console.log(data);
});
socket.emit('echo', 'this is a message');
function submit1() {
var file = $("#file_input")[0].files[0];
var Name = file.name;
var reader = new FileReader();
reader.onload = function (evnt) {
console.log(event)
socket.emit('join', { 'Name': Name, Data: evnt.target.result });
}
reader.readAsArrayBuffer(file);
}
</script>
</body>
</head>
服务器
var http = require('http')
var fs = require('fs');
var express = require('express')
var socketio = require('socket.io')
var app = express(server)
var server = http.Server(app)
var io = socketio(server)
app.get('/', function(req, res){
res.sendFile(__dirname + '/client.html')
});
io.on('connection', function(socket){
var writeStream = fs.createWriteStream(__dirname +'/12.mkv');
socket.on('echo', function(data){
socket.emit('echo', data);
});
socket.on('join', function(chunk){
chunk = chunk.Data;
writeStream.write(chunk);
})
});
server.listen(8080);