我想在一个客户端和一个服务器之间传输文件。 我只使用stream和fs模块。
目前,我可以转移" .txt"文件完美,但它不适用于" .mp3"文件(例如)。的确," .mp3"的大小。从服务器保存的文件大小比原始文件大。 (例如:9,2 mo而不是5mo)我无法打开它。
这是我的服务器部分:
var net = require('net');
var util = require("util");
var fs = require('fs');
var receive_object = {};
var server = net.createServer(function (socket){
console.log('server connected');
socket.write('hello\r\n');
//socket.pipe(socket);
socket.on('data',function(data){
if(data.toString() == "upload_demand"){
socket.write("upload_demand_ok");
}else if(data.toString().indexOf("fileName")>-1){
data = data.toString().replace("fileName","");
fs.exists("server_"+data, function(exists){
if(exists) socket.write("fileName_ko");
else{
receive_object.fileName = data;
receive_object.writable = fs.createWriteStream("server_"+receive_object.fileName);
receive_object.contenu = "";
socket.write("fileName_ok");
}
});
}else if(data.toString().indexOf("size")>-1){
data = data.toString().replace("size","");
receive_object.size_total = data;
receive_object.size_received = 0;
socket.write("size_ok");
}/*else if(data.toString()=="upload_success"){
receive_object.writable.write(receive_object.contenu);
console.log('It\'s saved!');
}*/
else{
receive_object.size_received += 64*1024;
socket.write("uploading"+parseInt(receive_object.size_received/receive_object.size_total*100));
receive_object.contenu += data;
}
});
socket.on('end', function() {
console.log('client disconnected');
receive_object.writable.write(receive_object.contenu);
});//socket on
});
/**
let's listening the server
*/
server.listen(1337, '127.0.0.1',function(){
console.log("server bound on port %j and on address %j",server.address().port,server.address().address);
});
这是我的客户部分:
var util = require("util");
var net = require("net");
var fs = require('fs');
var fileName = "exemple.txt";
/**
client connecting to the corresponding port Server
*/
var client = net.connect({port: 1337}, function () {
util.log("Client connected");
fs.exists(fileName, function(exists) {
if (exists) {
fs.stat(fileName, function(error, stats) {
transfer_object = {};
transfer_object.fileName = fileName;
transfer_object.size = stats.size;
transfer_object.stream = fs.createReadStream(fileName, { bufferSize: 64 * 1024 });
transfer_object.progression = 0;
client.write("upload_demand");
});
}else{
console.log("fileNotFound");
}
});
});
client.on('data', function(data) {
if(data=="upload_demand_ok"){
console.log(''+data);
client.write("fileName"+transfer_object.fileName);
}
else if(data == "fileName_ok"){
console.log(''+data);
client.write("size"+transfer_object.size)
}else if(data == "fileName_ko"){
console.log('ficher deja existant');
}else if(data == "size_ok"){
console.log(''+data);
transfer_object.stream.pipe(client,{ end: true });
}else if(data.toString().indexOf("uploading")>-1){
data = parseInt(data.toString().replace("uploading",""));
progressbar(data);
//if(data>=100) client.write('upload_success');
}
});
/**
client receive end event from server
*/
client.on('end', function() {
console.log('client disconnected');
});
function progressbar(int){
if(int>=0 && int<=20 && transfer_object.progression == 0){
transfer_object.progression = 1;
process.stdout.write("progression : [=");
}
else if(int>20 && int<=40 && transfer_object.progression == 1){
transfer_object.progression = 2;
process.stdout.write("==");
}else if(int>40 && int<=60 && transfer_object.progression == 2){
transfer_object.progression = 3;
process.stdout.write("==");
}else if(int>60 && int<=80 && transfer_object.progression == 3){
transfer_object.progression = 4;
process.stdout.write("==");
}else if(int>80 && int<=100 && transfer_object.progression == 4){
transfer_object.progression = 5;
process.stdout.write("=]");
}
}
答案 0 :(得分:0)
txt文件有utf8编码和mp3 - 二进制编码。 您可以按照http://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback
中的说明设置可用于流的编码根据http://nodejs.org/api/buffer.html#buffer_buffer
utf8
用于txt,binary
用于mp3数据(但手册不建议使用它)