0

I have a URI-data string that looks like this:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2w..... and so on

I need to save this to a jpg on the server, and using only Javascript. As a last resort, I could use the client to write this to a canvas and then read the image from there... but I'd really like to be able to do it on the server. For some reason, this is proving to be very complicated indeed.

Ideas??

Marc
  • 1,812
  • 4
  • 23
  • 36
  • I don't understand... is this a one-time thing? How do you get the base64 to the server? – JDB Mar 14 '17 at 21:46
  • Please share your code, what you expect it to do, and what it actually does, including any relevant errors or logs. – jcaron Mar 14 '17 at 21:46
  • 1
    send the server the string, keep the text to the right of the comma, un-base64 it and save. – dandavis Mar 14 '17 at 21:51

1 Answers1

1

Thanks for the tips, everyone! Here is the bit of code that finally did the trick:

    var fs = require("fs");
    var strippedPhotoUri = imageData.substring("data:image/jpeg;base64,".length);
    var buf = new Buffer(strippedPhotoUri, "base64");
    fs.writeFileSync(filename, buf, "base64", function(err) {
        if (err) {
            console.error("error: " + err);
        }
    });
Marc
  • 1,812
  • 4
  • 23
  • 36