Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6803743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:22:54+00:00 2026-05-26T19:22:54+00:00

I’ve been trying to handle saving images POSTed to nodeJS (and the express framework)

  • 0

I’ve been trying to handle saving images POSTed to nodeJS (and the express framework) to a database, and have been having some trouble. Ignoring all the web processing, I think that I’ve narrowed down the problem to the way base64 encoding is happening in node. I believe that the oversimplified example below should work, but the output image is always corrupted.

The example (1) loads in an image (2) saves a copy of if (image_orig) to confirm that node can read the file properly. This always works. (3) I take the image and base64 encode its contents, (4) then decode it. The final output image (image_decoded) is always corrupted.

Help!
(node.js 0.6.0 on OSX Lion)

console.log("starting");
process.chdir(__dirname);

var fs = require("fs");

var image_origial = "image.jpg";
fs.readFile(image_origial, function(err, original_data){
    fs.writeFile('image_orig.jpg', original_data, function(err) {});
    var base64Image = new Buffer(original_data, 'binary').toString('base64');
    var decodedImage = new Buffer(base64Image, 'base64').toString('binary');
    fs.writeFile('image_decoded.jpg', decodedImage, function(err) {});
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:22:54+00:00Added an answer on May 26, 2026 at 7:22 pm

    I think you are misunderstanding the usage of the encoding argument a bit. If you are going to specify encoding ‘binary’, then you need to do it consistently. But really you don’t need it at all. You seem to be confusing the usage of Buffer vs binary strings.

    // This tells node to load the file into a Buffer 'original_data' because you
    // have not specified an encoding for the returned values. If you provided an
    // encoding, then original_data would be a string with that encoding.
    fs.readFile(image_origial, function(err, original_data){
    
        // This tells node to take that buffer, and write it to the new filename.
        // Again no encoding is provided, so it will assume a Buffer or utf8 string.
        fs.writeFile('image_orig.jpg', original_data, function(err) {});
    
        // This tells node to create a new buffer from the old buffer, which means
        // it will iterate over original_data copying the bytes one at a time. But
        // they will be identical buffers. It will ignore the 'binary' argument
        // since the object you are passing isn't a string.
        // Then it encodes the content of that Buffer to base64, which is fine.
        var base64Image = new Buffer(original_data, 'binary').toString('base64');
    
        // Here you decode the base64 to a buffer, which is fine, but then you
        // convert the buffer into a string with encoding 'binary'. This means that
        // it is a string object whose code points are bytes of the buffer.
        var decodedImage = new Buffer(base64Image, 'base64').toString('binary');
    
        // Here you try to write that String object to a file. Since the argument you
        // have given is a string and you have not given an encoding argument for the
        // write command, then it will assume that 'utf8' is the encoding. It will try to
        // decode your binary string into a utf8 encoded buffer, and write that buffer.
        // This will cause it to fail because that encoding conversion is wrong.
        // Really through, 'binary' is just wrong to use. Buffers are already binary.
        fs.writeFile('image_decoded.jpg', decodedImage, function(err) {});
    });
    

    This next example will work but is very inefficient because changing encodings all the time isn’t needed, but I just want to show it to be clear. If you really DID want to have a specific encoding, you need to make sure you are consistent. Every one of those functions has an encoding argument.

    fs.readFile(image_origial, 'binary', function(err, original_data){
        fs.writeFile('image_orig.jpg', original_data, 'binary', function(err) {});
        var base64Image = new Buffer(original_data, 'binary').toString('base64');
        var decodedImage = new Buffer(base64Image, 'base64').toString('binary');
        fs.writeFile('image_decoded.jpg', decodedImage, 'binary', function(err) {});
    });
    

    This is the right way to do it. Keep everything as a Buffer, except when you make it base64.

    fs.readFile(image_origial, function(err, original_data){
        fs.writeFile('image_orig.jpg', original_data, function(err) {});
        var base64Image = original_data.toString('base64');
        var decodedImage = new Buffer(base64Image, 'base64');
        fs.writeFile('image_decoded.jpg', decodedImage, function(err) {});
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.