Skip to main content
Question

Uploading files

  • May 22, 2025
  • 3 replies
  • 43 views

Forum|alt.badge.img

If you want to use node.js to upload a file to the box, use
However, if I want to upload base64 data instead of a file path, what should I do?
Also, the fileType can be String or binary, but if binary, which buffer?
Please let me know.

3 replies

Forum|alt.badge.img

Hello, 

Have you tried something like this post from Stack Overflow? 

Thanks, 

Alex, Box Developer Advocate


Forum|alt.badge.img

It does look like our sdk example only shows a file path... Ill send this to that team too. 

Alex


Forum|alt.badge.img

Hi 1908936779225,

I'll preparing an update to the docs, here is something that worked for me when I wanted to upload a base64 content using Box Node SDK

var {Readable, ReadableOptions} = require('stream');
var fs = require('fs');
var base64Content = 'TXkgY29udGVudAo='; // your base64 content
var base64Buffer = Buffer.from(base64Content, 'base64');
// we are using just Readable to create a stream, but you can use any library you want
var stream = new Readable()
stream._read = () => {
stream.push(base64Buffer);
stream.push(null);
};
// you have to pass options and define content length
var options = {
content_length: Buffer.byteLength(base64Content, 'base64')
};
// I'm uploading file to my root folder to a file 'My Base64 File.txt'
client.files.uploadFile('0', 'My Base64 File.txt', stream, options);