如何从JS中的ArrayBuffer写入文件 [英] How to write a file from an ArrayBuffer in JS
问题描述
原理是将客户端上的文件从ArrayBuffer中分离出来,通过一个Meteor.method发送到服务器的4096位小数据包中。 下面的简化代码是向服务器发送块的客户端的一部分,直到偏移量达到 data.byteLength : p>
//数据是一个ArrayBuffer
var total = data.byteLength;
var offset = 0;
var upload = function(){
var length = 4096; //块大小
//调整最后块大小
if(offset + length> total){
length = total - offset;
}
//我使用Uint8Array来创建块
//,因为它可以本地传递给Meteor.method
var chunk = new Uint8Array数据,偏移量,长度);
if(offset< total){
//发送块到服务器并告诉它要附加到
的文件Meteor.call('uploadFileData',fileId,如果(!err){
offset + = length; $ b $ upload();
}
}
}
上传(); //开始上传
下面的代码是接收块并将其写入文件系统的服务器上的部分:
var fs = Npm.require('fs');
var Future = Npm.require('fibers / future');
Meteor.methods({
uploadFileData:函数(fileId,chunk){
var fut = new Future();
var path ='/ uploads /'+ fileId;
$ b $ //我试过没有成功
chunk = String.fromCharCode.apply(null,chunk);
//如何将Uint8Array的块写入磁盘?
fs.appendFile(path,块,二进制,函数(ERR){
如果(ERR){
fut.throw(ERR);
} else {
fut.return(chunk.length);
}
});
return fut.wait();
}
});
我无法将有效的文件写入磁盘,实际上文件已保存,但无法打开,当我看到文本编辑器中的内容,它是类似于原始文件(例如一个JPG),但有些字符是不同的,我认为这可能是一个编码问题,因为文件大小是不一样的,但我不不知道如何解决这个问题...
解决方案
保存文件和使用Uint8Array对象创建一个新的Buffer一样简单:
$ $ p $ code $ chunk是Uint8Array对象
fs.appendFile(path,new Buffer(chunk),function err){
if(err){
fut.throw(err);
} else {
fut.return(chunk.length);
}
});
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...
Saving the file was as easy as creating a new Buffer with the Uint8Array object :
// chunk is the Uint8Array object
fs.appendFile(path, new Buffer(chunk), function (err) {
if (err) {
fut.throw(err);
} else {
fut.return(chunk.length);
}
});
这篇关于如何从JS中的ArrayBuffer写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!