Saving Image with JavaScript / Node to FileSystem


#1

Hi,

I write some code for image uploading and I have a problem with saving the image file to local file system.

As you can see on screenshots Code creates a file on my location but a file is not readable.

Do I miss something in a process of saving?

Here is Client and Server Code Screenshot:

Here is error and you can see in back encoding/decoding result it seems okay.


#2

SOLUTION:

Client

readAsDataURL has base64 encoded data in the format of

data:image/jpeg;base64,/9j/4AAQSkZJRgABA…
So you need to get rid of the mime type and encoding information at the front.

contents = contents.split(’,’)[1];
Now you can send this base64 encoded data to server.

Server

Since you’re receiving base64 encoded data, you can convert it buffer and write to file:

fs.writeFile(filepath, Buffer(argument,‘base64’), err => {
//
})

Hassansin give me correct solution on: http://stackoverflow.com/questions/36815714/node-js-meteor-js-image-file-upload-image-corrupted/36817216#36817216