29

I'm trying to encode an image using base64 in Node.JS to pass along to the PostageApp API as an attachment. I thought I had it working but it attaches a 1K file that isn't exactly what I was looking for.

Here's my code:

 var base64data;

 fs.readFile(attachment, function(err, data) {
   base64data = new Buffer(data).toString('base64');
 });

And here's the part of the API call I am making:

 attachments: {
   "attachment.txt" : {
     content_type: "application/octet-stream",
     content: base64data
   },
 }

I'm a bit lost, not being so great with Node, but I thought it would work. Any help would be appreciated!

1
  • @Jim Schubert, that advice is wrong AND bad. First, readFile reads the whole file, not chunks. Second, blocking code would block the entire server, stupid idea. Commented Aug 15, 2011 at 18:14

3 Answers 3

47
fs.readFile(attachment, function(err, data) {
   var base64data = new Buffer(data).toString('base64');
   [your API call here]
});

It takes some time until the results are there, so by the time you've got the data, the outer scopes execution is already over.

Sign up to request clarification or add additional context in comments.

7 Comments

Unfortunately, I pass base64data into a payload, which is essentially a JSON hash, which I then POST to the API server. Would this be preferable to what I'm doing?
@JonLim, this is basically the only way to do it. You'll have to do all that stuff ("pass base64data into a payload, which is essentially a JSON hash, which I then POST to the API server") inside the callback.
Ah. No other way around it? That sucks, cause the attachments parameter is optional, so if there are no attachments, it doesn't get included. Thanks though!
@JonLim, in that case, create a named function for your callback and fire it directly if there are no attachments.
Awesome, thank you! Another quick question though: what if I had multiple files to attach?
|
13

Just specify "base64" as the encoding. Per the docs:

If no encoding is specified, then the raw buffer is returned.

fs.readFile(attachment, {encoding: 'base64'}, function(err, base64data) {
   [your API call here]
});

2 Comments

Isn't encoding for readFile the encoding of the attachment and not what you want to encode to?
in the document, The encoding can be any one of those accepted by Buffer. though it is of createReadStream. I've tried what @hyphenthis said and it worked.
0

As of March 2023, I found this solution to be useful because the accepted answer is now deprecated. Use new Buffer.from along with base64 parameter instead of new Buffer does the trick.

base64data = new Buffer.from(data, 'base64').toString('base64');

1 Comment

new is not needed. Buffer.from already creates a new Buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.