Wall's Corners Wall's Corners Author
Title: Convert An Uploaded Image To A Base64 String In Node.js
Author: Wall's Corners
Rating 5 of 5 Des:
Not too long ago I wrote about uploading files to a Node.js server that was making use of the Multer middleware.  In that scenario the file...

Not too long ago I wrote about uploading files to a Node.js server that was making use of the Multer middleware.  In that scenario the files were saved to the file system and even though I didn’t demonstrate it, in theory the file metadata would be stored in a database.  What if you wanted to store the files in a database as a base64 string instead of on the file system?

We’re going to take a look at what it would take to allow file uploads in Node.js and convert them into base64 strings for database storage.

Let me start by saying that although this is possible, it is best to store your files on the filesystem and not within the database.  Storage on the filesystem is cheaper than database storage.

We’re going to start by creating a fresh Node.js project using NPM.  Create a project directory somewhere and from your Command Prompt (Windows) or Terminal (Mac and Linux), execute the following:

For simplicity, our entire app will exist in the app.js file.  The command npm init -y will create our package.json file which will house all of the project dependencies.

Now we need to install such dependencies.  Execute the following from your Command Prompt or Terminal:

With everything ready to go we can start coding.  Add the following to the project’s app.js file:

This code was pretty much taken exactly from my previous post on file uploads with Multer and Angular 2.  However notice the following line:

We’re including this dependency because we need to be able to read the file from the file system.  The goal here is to accept the file upload, save it to the file system temporarily, read it to convert into base64, then do cleanup on the file system.

Take a look at the following snippet of code:

This snippet will replace the /upload endpoint in our app.js file.  The first thing we do is loop through the array of files that were provided.

We’re going to push our own modified file data to a new array that will eventually be returned.

The file data that we want to keep is the original file name, file size, and then the base64 string representing the file itself.  To get the base64 string we read the file synchronously from the file system, add it to a buffer and then convert it to a string.  You certainly could use an asynchronous method for reading from the file system, but I chose the synchronous method for simplicity.

After we push to our array we can delete the file from the file system.  We don’t need to do this in a synchronous fashion so instead of holding up our execution we just unlink the files asynchronously.

Conclusion

We saw how to upload files to a Node.js server that uses the Multer middleware.  Previously we only cared about leaving the file on the filesystem, but this time around we converted it to a base64 string.  This might be useful if you would rather everything be contained in your database.  It might also be useful if you have a mobile application with sync.

Keep in mind, I’d actually recommend leaving the files on your file system.  File system storage is cheap, its quick, and in my opinion better.

 

Share This:

View more at: http://bit.ly/1XReQFr

About Author

Advertisement

Post a Comment

 
Top