Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  165] [ 1]  / answers: 1 / hits: 67111  / 9 Years ago, thu, july 23, 2015, 12:00:00

I am trying to write a file uploader for Meteor framework.
The principle is to split the fileon the client from an ArrayBuffer in small packets of 4096 bits that are sent to the server through a Meteor.method.



The simplified code below is the part of the client that sends a chunk to the server, it is repeated until offset reaches data.byteLength :



// data is an ArrayBuffer
var total = data.byteLength;
var offset = 0;

var upload = function() {
var length = 4096; // chunk size

// adjust the last chunk size
if (offset + length > total) {
length = total - offset;
}

// I am using Uint8Array to create the chunk
// because it can be passed to the Meteor.method natively
var chunk = new Uint8Array(data, offset, length);

if (offset < total) {
// Send the chunk to the server and tell it what file to append to
Meteor.call('uploadFileData', fileId, chunk, function (err, length) {
if (!err) {
offset += length;
upload();
}
}
}
};
upload(); // start uploading


The simplified code below is the part on the server that receives the chunk and writes it to the file system :



var fs = Npm.require('fs');
var Future = Npm.require('fibers/future');

Meteor.methods({
uploadFileData: function(fileId, chunk) {
var fut = new Future();
var path = '/uploads/' + fileId;

// I tried that with no success
chunk = String.fromCharCode.apply(null, chunk);

// how to write the chunk that is an Uint8Array to the disk ?
fs.appendFile(path, chunk, 'binary', function (err) {
if (err) {
fut.throw(err);
} else {
fut.return(chunk.length);
}
});
return fut.wait();
}
});


I failed to write a valid file to the disk, actually the file is saved but I cannot open it, when I see the content in a text editor, it is similar to the original file (a jpg for example) but some characters are different, I think that could be an encoding problem as the file size is not the same, but I don't know how to fix that...


More From » node.js

 Answers
22

Saving the file was as easy as creating a new Buffer with the Uint8Array object :



// chunk is the Uint8Array object
fs.appendFile(path, Buffer.from(chunk), function (err) {
if (err) {
fut.throw(err);
} else {
fut.return(chunk.length);
}
});

[#65714] Monday, July 20, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eliza

Total Points: 732
Total Questions: 96
Total Answers: 86

Location: Guam
Member since Fri, Jun 18, 2021
3 Years ago
;