如何使用node.js将二进制数据写入文件? [英] How to write binary data to a file using node.js?

查看:2648
本文介绍了如何使用node.js将二进制数据写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图做 toDataUrl()的canvas,它给出 base64 数据。我想把它保存为 png 。我可以从base64获得转换后的二进制数据,但是我不能用NodeJs服务把它写到一个文件中。如果我把base64数据直接写到文件中,所有的数据都可以被写入,但它不能是 png 对吧?我想存储要存储的二进制数据。

代码段:

c $ c> var strData = this.drawingCanvas.getContext()。canvas.toDataURL();

var data = strData.replace(/ ^ data:image\ / \\\w +; base64,/,);

var imgData = this.decode(data); //解码(数据)定义如下
$ b $ this.call({filePath:'< path> /image.png',data:imgData},
{method:writeFile });`

`utf8decode:function(utftext){
var string =;
var i = 0;
var c = c1 = c2 = 0;

while(i< utftext.length){

c = utftext.charCodeAt(i);

if(c <128){
string + = String.fromCharCode(c);
i ++; (c> 191)&(c <224)){
c2 = utftext.charCodeAt(i + 1);
}
else if ((c& 31)< 6)|(c2& 63));
string + = String.fromCharCode
i + = 2;
}
else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2); ((c& 15)<< 12)|((c2& 63)<< 6)|(c3& 63));
i + = 3;
}

}

返回字符串;
},`

`_keyStr:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + / =,

decode:function(input){
var output =;
var chr1,chr2,chr3;
var enc1,enc2,enc3,enc4;
var i = 0;

input = input.replace(/ [^ A-Za-z0-9\ + \ / \ = = / g,);

while(i< input.length){

enc1 = this._keyStr.indexOf(input.charAt(i ++));
enc2 = this._keyStr.indexOf(input.charAt(i ++));
enc3 = this._keyStr.indexOf(input.charAt(i ++));
enc4 = this._keyStr.indexOf(input.charAt(i ++));

chr1 =(enc1 <2)| (enc2>> 4);
chr2 =((enc2& 15)<< 4)| (enc3>> 2);
chr3 =((enc3& 3)<< 6)| enc4;

output = output + String.fromCharCode(chr1);

if(enc3!= 64){
output = output + String.fromCharCode(chr2);

if(enc4!= 64){
output = output + String.fromCharCode(chr3);
}

}

output = this.utf8decode(output);

返回输出;

},`

/ ****************************** ****** *****
******************************** /

var WriteFileAssistant = function(){};

WriteFileAssistant.prototype.run = function(future,subscription){

var fs = IMPORTS.require('fs');
var filePath = this.controller.args.filePath;

var f = subscription.get();
f.result = {reply:data};

var fd = fs.openSync('< path> /image.png','a +');
// var data = fs.writeSync(fd,g,null,encoding ='utf8');

//this.controller.args.data - 图像数据(二进制)
var buff = new Buffer(this.controller.args.data,'binary');
//也用'base64'

fs.write(fd,buff,0,buff.length,0,function(err,written){

});

var f = subscription.get();
f.result = {reply:data};


解决方案

。节点Buffer对象以base64作为输入,为你完成所有的解码工作。



你可以从base64字符串中去除数据:image ...部分,将数据发送到您的WriteFileAssistant。

  var strData = this.drawingCanvas.getContext()。canvas.toDataURL(); 
var imgData = strData.replace(/ ^ data:image \ / \\\w +; base64,/,);
this.call(
{
filePath:'/ media / internal / Collage / image.png',
data:imgData
},
{
方法:writeFile
}
);

WriteFileAssistant只需要获取base64字符串并将其作为参数传递给Buffer构造函数。另外,在openSync调用中使用'a +'也会导致事情中断。

  var WriteFileAssistant = function(){} 

WriteFileAssistant.prototype.run = function(future,subscription){

var fs = IMPORTS.require('fs');
var filePath = this.controller.args.filePath;

var fd = fs.openSync('< path> /image.png','w');

var buff = new Buffer(this.controller.args.data,'base64');
$ b $ fs.write(fd,buff,0,buff.length,0,function(err,written){

});





Buffer需要一个字符串和一个编码,然后使用编码值来处理将字符串转换为一系列的字节,所以当你告诉它字符串是base64的时候,它会为你解码base64并创建正确解码的字节数组来写入文件。


I am trying to do toDataUrl() of canvas, and it gives base64 data. I want to store it as a png. I can get the converted binary data from base64, but I cant write it to a file using NodeJs service.

If I write base64 data directly to the file, all data can be written, but it cannot be a png right?. I want to store the binary data to be stored. How to do it?

Code snippet:

var strData = this.drawingCanvas.getContext().canvas.toDataURL();

var data = strData.replace(/^data:image\/\w+;base64,/, "");

var imgData = this.decode(data); // decode(data) is DEFINED BELOW

this.call({filePath:'<path>/image.png', data: imgData}, 
            {method:"writeFile"});`

`utf8decode : function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while ( i < utftext.length ) {

        c = utftext.charCodeAt(i);

        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        }
        else if((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i+1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else {
            c2 = utftext.charCodeAt(i+1);
            c3 = utftext.charCodeAt(i+2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }

    }

    return string;
},`

`_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

decode : function (input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    while (i < input.length) {

        enc1 = this._keyStr.indexOf(input.charAt(i++));
        enc2 = this._keyStr.indexOf(input.charAt(i++));
        enc3 = this._keyStr.indexOf(input.charAt(i++));
        enc4 = this._keyStr.indexOf(input.charAt(i++));

        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        output = output + String.fromCharCode(chr1);

        if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
        }

    }

    output = this.utf8decode(output);

    return output;

},`

/**************************************************
wRITEfILEaaSSISTANT.JS
***************************************************/

var WriteFileAssistant = function(){};

WriteFileAssistant.prototype.run = function(future, subscription) {

var fs = IMPORTS.require('fs');
var filePath = this.controller.args.filePath;

var f = subscription.get();
f.result = {reply: data};

var fd =  fs.openSync('<path>/image.png', 'a+');
//var data = fs.writeSync(fd, g, null, encoding='utf8');

//this.controller.args.data - Image data (binary)
var buff = new Buffer(this.controller.args.data, 'binary');
//tried also with 'base64'

fs.write(fd, buff, 0, buff.length, 0, function(err,written){

});

var f = subscription.get();
f.result = {reply: data};

解决方案

You are making things much harder than they need to be. The node Buffer object takes base64 as input and does all of that decoding for you.

You can just strip the data:image... part from the base64 string and pass that data to your WriteFileAssistant.

var strData = this.drawingCanvas.getContext().canvas.toDataURL();
var imgData = strData.replace(/^data:image\/\w+;base64,/, "");
this.call(
  {
    filePath:'/media/internal/Collage/image.png',
    data: imgData
  },
  {
    method:"writeFile"
  }
);

The the WriteFileAssistant just needs to take the base64 string and pass that as an argument to the Buffer constructor. Also, having 'a+' on the openSync call will break things too.

var WriteFileAssistant = function(){};

WriteFileAssistant.prototype.run = function(future, subscription) {

  var fs = IMPORTS.require('fs');
  var filePath = this.controller.args.filePath;

  var fd =  fs.openSync('<path>/image.png', 'w');

  var buff = new Buffer(this.controller.args.data, 'base64');

  fs.write(fd, buff, 0, buff.length, 0, function(err,written){

  });
}

Buffer takes a string and an encoding, then it uses the encoding value to process the string into a series of bytes, so when you tell it that the string is base64, it will decode the base64 for you and create the proper decoded array of bytes to write to the file.

这篇关于如何使用node.js将二进制数据写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆