Handling file uploads
We cannot process an uploaded file in the same way we process other POST data. When a file input is submitted in a form, the browser processes the file into a multipart message.
Multipart was originally developed as an email format allowing multiple pieces of mixed content to be combined into one message. If we intuitively attempted to receive the upload as a stream and write it to a file, we would have a file filled with multipart data instead of the file or files themselves. We need a multipart parser, the writing of which is more than a recipe can cover. So instead we'll be using the well-known and battle-tested formidable
module to convert our upload data into files.
Getting ready
Let's create a new uploads
directory for storing uploaded files and get ready to make modifications to our server.js
file from the previous recipe.
We'll also need to install formidable
as follows:
npm install formidable@1.x.x
Finally, we'll make some changes to our form.html
from the...