Uploading and Downloading Files: Buffering in Node.js

By June 17, 2019 Uncategorized

Of the various ways to transfer files to and from Oracle Database, buffering in Node.js is the easiest to implement. In this post, you’ll learn how to perform file uploads and downloads using buffered binds and fetches.



Buffering means that a file’s contents are fully materialized (buffered) in Node.js before being transferred to either the database or the client. This is as opposed to streaming, where the contents are passed along intermittently. Streaming is more efficient with respect to memory utilization, but it is also more complex to implement.

When buffering in Node.js, it’s essential to consider the amount of memory available to the Node.js process, the number of concurrent file uploads and downloads, and the size of the files being transferred. For example, if your Node.js process has 1 GB of memory and you have at most 10 concurrent transfers, and the max file size is 50 MB, then buffering may be ideal. But if you double the concurrent transfers or the file sizes, then you may run into problems. Also, the underlying Oracle Call Interface (OCI) libraries limit buffers to 1 GB, so you’ll need to use streaming APIs for files larger than that.

Uploading files

Controller logic

In a typical Node.js web server, the request object passed to the request listener is an instance of the http.IncomingMessage class. This class implements the Readable Stream interface, which allows you to get incoming data via the data event.

To buffer the data in Node.js, all you need to do is push the data into an array. Once the data is done streaming in, it can be passed to the database logic to be inserted. Here’s what the controller logic that handles an HTTP post looks like using buffering.


  • Lines 3-12: Several variables are declared, including an array named contentBuffer which will be used to buffer the data as it streams into Node.js. Note that the file name and content type are sent from the client as HTTP headers since the body of the request is reserved for the file content.
  • Lines 14-27: Each time the data event fires, the chunk of raw data passed in is added to the contentBuffer array. Also, the amount of data buffered is checked to see if it exceeds the cap set via maxFileSize. If so, the streaming is stopped and an error is returned to the client.
  • Lines 30-32: A no-op listener is added to the aborted event. This event would fire if the client aborted the request, say by refreshing the browser while in the middle of an upload. When buffering, it’s not necessary to do anything with this event since the data was never passed to the database APIs. Eventually, garbage collection will free the memory used.
  • Lines 34-49: When the end event fires, the content buffer array is converted into a scalar Buffer and then passed along to the database API to be inserted. If successful, the id of the file is returned to the client, otherwise a generic error is returned.
Database logic

Once the controller logic has passed along the buffered file content and related metadata, the database logic can execute the SQL to insert it. This is where the driver’s ability to accept buffers as bind variables comes in very handy.


  • Lines 1-10: A SQL insert statement is declared. The bind variable named :content_buffer is mapped to the blob_data column of the table used to store files.
  • Lines 12-26: A variable named binds is declared for the bind definitions. Note that contentBuffer is a simple in bind (just like fileName and contentType) – the driver does all the heavy lifting. Finally, the data is inserted and the id of the row is returned.
Downloading files
Controller logic

When downloading a file from the database, the controller logic requests the entire file contents from a database API. Once the file data is obtained, it’s sent to the client in a single shot.


  • Lines 3-10: The id of the file to be downloaded is parsed from the request parameters and passed along to the get method of the database API to fetch the entire file.
  • Lines 13-22: If the database API successfully returns a row, then the HTTP response status is set to 200 OK. Next, HTTP headers (including metadata related to the file) are added to the response. Smart clients, such as browsers, know how to use such headers when downloading files. Finally, the file contents are sent to the client by passing the file buffer to the send method of the response object.
Database logic

When it comes to fetching the file content back out of the database, the fetchInfo object can be used to fetch the data as a fully materialized buffer.


  • Lines 1-7: A SQL statement to fetch the file data is declared.
  • Lines 9-24: Within the get function, variables are declared for the bind definitions and the execute options. Within the execute options, fetchInfo is used to override the default return type of a BLOB column (Lob/readable stream) to be a Node.js Buffer. Finally, the query is executed and the result rows are passed back to the controller logic.

I hope you agree that the driver’s buffer APIs make inserting and fetching file contents very simple. All it takes is a few lines of code to implement file upload and download capabilities to your application. The only drawbacks are related to file size limits and scalability. In the next part, I’ll show you how to use streaming APIs to address both issues.

One Comment