By AliHmede


2019-09-11 09:24:26 8 Comments

I'm sending an image encoded as base64 through sockets and decoding is not working. The file that must contain the new image is written as base64 instead of a jpg file.

encoding socket:

function encode_base64(filename) {
  fs.readFile(path.join(__dirname, filename), function (error, data) {
    if (error) {
      throw error;
    } else {
      console.log(data);
      var dataBase64 = data.toString('base64');
      console.log(dataBase64);


      client.write(dataBase64);
    }
  });
}

rl.on('line', (data) => {
    encode_base64('../image.jpg')

})

decoding socket:

function base64_decode(base64str, file) {

   var bitmap = new Buffer(base64str, 'base64');

   fs.writeFileSync(file, bitmap);
   console.log('******** File created from base64 encoded string ********');
  }


client.on('data', (data) => {


    base64_decode(data,'copy.jpg')


});

// the first few characters in the new file 
//k1NRWuGwBGJpmHDTI9VcgOcRgIT0ftMsldCjFJ43whvppjV48NGq3eeOIeeur

3 comments

@Sandeep Patel 2019-09-11 09:36:18

Change encode function like below. Also, keep in mind new Buffer() has been deprecated so use Buffer.from() method.

function encode_base64(filename) {
  fs.readFile(path.join(__dirname, filename), function (error, data) {
    if (error) {
      throw error;
    } else {
      //console.log(data);
      var dataBase64 = Buffer.from(data).toString('base64);
      console.log(dataBase64);
      client.write(dataBase64);
    }
  });
}

And decode as Below :

function base64_decode(base64Image, file) {
  fs.writeFileSync(file,base64Image);
   console.log('******** File created from base64 encoded string ********');

}

client.on('data', (data) => {
    base64_decode(data,'copy.jpg')
});

@AliHmede 2019-09-11 09:39:33

the terminal of the receiving socket gave this error: TypeError: base64str.split is not a function at base64_decode (C:\Users\HP\Desktop\Node\sockets\client2.js:17:28)

@Sandeep Patel 2019-09-11 09:41:06

is data string?

@AliHmede 2019-09-11 09:45:14

on " client.write(dataBase64) " the data is string

@AliHmede 2019-09-11 09:55:41

the terminal wrote it "File created from base64 encoded string" but the jpg file is still written as base64 (no image is made)

@Sandeep Patel 2019-09-11 10:05:11

Can you give one more try I have changed decode method code a bit

@AliHmede 2019-09-11 10:08:01

thank you a lot for helping me but the file is still writing a base64 characters

@Sandeep Patel 2019-09-11 10:20:01

can you check data type of 'data' at receiving end?

@AliHmede 2019-09-11 10:21:47

the data type is buffer

@Sandeep Patel 2019-09-11 10:27:06

is it TCP socket for writing buffer?

@AliHmede 2019-09-11 10:30:03

yes, a the encoding socket is client sending to server then send it to another client. i am using this: var net = require('net'); var client = new net.Socket();

@AliHmede 2019-09-11 10:52:55

that made it! thank you

@Sandeep Patel 2019-09-11 11:01:15

@AliHmede, I am happy to help you.

@Pushprajsinh Chudasama 2019-09-11 09:37:15

You can decode the base64 image using following method .

EDITED

To strip off the header

let base64String = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA'; // Not a real image
// Remove header
let base64Image = base64String.split(';base64,').pop();

To write to a file

import fs from 'fs';
fs.writeFile('image.png', base64Image, {encoding: 'base64'}, function(err) {
    console.log('File created');
});

Note :- Don’t forget the {encoding: 'base64'} here and you will be good to go.

@AliHmede 2019-09-11 09:46:49

it gave me an error: DeprecationWarning: Buffer() is deprecated due to security and usability issues so i changed it to Buffer.from(data, "base64") but the file is still written in base64 not as jpg

@Pushprajsinh Chudasama 2019-09-11 09:50:37

try to give you another solution .

@Pushprajsinh Chudasama 2019-09-11 10:40:46

I have updated my code , you can check it out . @AliHmede

@Avraham 2019-09-11 09:52:11

It seems that the decoding function base64_decode gets the data as a buffer. Thus, the encoding argument in new Buffer(base64str, 'base64') is ignored. (Compare the docs of Buffer.from(buffer) vs Buffer.from(string[, encoding])).

I suggest to convert to a string first

function base64_decode(base64str, file) {

   var bitmap = new Buffer(base64str.toString(), 'base64');

   fs.writeFileSync(file, bitmap);
   console.log('******** File created from base64 encoded string ********');
  }

@AliHmede 2019-09-11 09:59:38

strange characters appeard in the file "not base64 even" ���

@Avraham 2019-09-11 10:05:55

Right. Actually it's another issue, because k1NRWuGwBGJpmHDTI9VcgOcRgIT0ftMsldCjFJ43whvppjV48NGq3eeOIeeu‌​r is not a valid base64 string. Try to console.log the first 10 characters written to the client with the first 10 chars received by the base64_decode function.

Related Questions

Sponsored Content

36 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

58 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

82 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5952065 View
  • 7420 Score
  • 82 Answer
  • Tags:   javascript arrays

62 Answered Questions

[SOLVED] How to check whether a checkbox is checked in jQuery?

39 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

58 Answered Questions

[SOLVED] How do I redirect to another webpage?

55 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

3 Answered Questions

57 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

86 Answered Questions

[SOLVED] How do JavaScript closures work?

Sponsored Content