Text File Uploads with Multer — The Complete Guide
Multer is a Node.js middleware for handling multipart/form-data
, which is primarily used for uploading files.
In this article, I will be covering how to read the contents of a text file with multer file upload.
The article is divided into three parts -
1. Setting up the Frontend (HTML)
2. Setting up the Backend (ExpressJS)
3. Reference links for image upload with Multer.
The following cases are being covered in this article -
- Form accepting only a single file (.txt)
- Form accepting only a single file (.txt) along with other input fields.
- Form accepting multiple files (Different input fields)
- Form accepting multiple files along with other input fields.
Basics of Multer -
Multer is a Node.js middleware. Middleware is a piece of software that connects different applications or software components. In Express, middleware processes and transforms incoming requests to the server. In our case, Multer acts as a helper when uploading files.
So let’s get started with the implementation!
Project Setup
1) Setting up the frontend -
Let’s create a basic HTML form that accepts a single text file upload with a Submit button.
IMPORTANT — Do not forget to put
enctype=“multipart/form-data”
in your form.
The /uploadFile
is the backend route which we will write while setting up the backend.
Our index.html
will look something like this on opening in the browser.
2) Setting up the backend -
In your project directory, create a new file namedserver.js
followed by an npm init
It’s finally time to install our dependencies — Express and of course Multer!
Install the dependencies using the following command:
npm i express multer --save
In our server.js
we will now setup a basic server up on port 3000 with the following code—
Next, let’s configure the server for accepting files from the client.
We will import the multer
library (line #2) and initialize its storage (line # 8 and line #9)
Now, we will be adding a route for file upload —
Note — This is the same
/uploadFile
route which we added to our HTML form while setting up the frontend
Line #23 in the above code is the line which reads the content from the text file and stores it in variable multerText
That’s it! You can use this variable which stores the actual contents of the text file and use it for further processing or store it in the database.
Next, let’s add validation for file MIME-type text- For this, we update the multer configuration with the following fileFilter
code block that checks for a file’s MIME type (in our case — text)
const fileFilter = (req, file, cb) => {
if (file.mimetype == 'text/*') {
cb(null, true);
} else {
cb(null, false);
}
}
const upload = multer({
fileFilter,
storage
});
This is how our final code including backend validation for text file looks like —
To run this — we simply do a node server.js
in our project directory and our app will be live on http://localhost:3000
Coming to the next part of our tutorial, we will now modify the form and the backend route to accept a single file (.txt) along with other input fields.
So we modify our HTML form and add an input field name
with type text.
The backend change will just involve adding the req.body
parameter to the result (line # 39) in the below snippet.
Coming to the next part of our tutorial, we will now modify the form and the backend route to accept multiple text files in different input fields. (Something like the below screenshot)
We will modify the HTML form to include two input fields with different names myFile1 and myFile2.
Now at the backend, instead of accepting a single file in upload.single
, we change it to upload.fields
(line #29) and pass in an array with the same name attributes used in our HTML form.
The last and the final part of our tutorial — we will now modify the form and the backend route to accept multiple text files along with an input field as well.
Along with our two file buttons, we accept an input as well on line #13
And at the backend, we pass the req.body.name
also in the result.
3) Reference link for image upload with Multer
https://code.tutsplus.com/tutorials/file-upload-with-multer-in-node--cms-32088
That’s it!
Hope you find this article helpful and gained some knowledge about file uploads using Multer in Node.js.
Thank You! :)