express-easy-fileuploader
v1.0.2
Published
easy express file uploader (laravel like)
Downloads
8
Maintainers
Keywords
Readme
Express Easy File Uploader
the hardest part about express is handling file uploads. there are a lot of packages for this purpose but all of them add extra layer of difficulty. our package express-easy-fileupload uses the old package express-fileupload and put a very powerful middleware to convert the manual code that the old library instruct you to write to a small function attached to the request object
Features
- Small function attached for each file separately (more flexibility)
- Handles both single and multiple files
- Can generate file name for the files if you want (optional)
- Check file existence in desk
- Can create the path of folder you pass to save the file if not exists (optional)
Install
npm i express-easy-fileupload
Usage
First of all you need to import the package and use add as middleware in your express app.
const fileEasyUpload = require("express-easy-fileuploader")
var app = express();
app.use(
fileEasyUpload({
app,
fileUploadOptions:{
limits: { fileSize: 500 * 1024 * 1024 },
}
})
);
the middleware receives object containing the following:
app
: the express application instancefileUploadOptions
: the old express-fileupload options (you can get it from this link)
Second you save the files in your express routes' handlers
you save the file by calling the method save
on the file name in the request object. save receives 2 parameters
directoryPath
fileName
: optional. the package can generate name for you (better not use it)app.post("/api/test",async function(req,res){ var path1 = await req.photo.save(directoryPath) var path2 = await req.cv.save(directoryPath) var pathes = [] for(var i=0;i<req.docs.length;i++){ pathes.push(await req.docs[i].save(directoryPath)) } console.log("photo is saved to",path1) console.log("cv is saved to",path2) console.log("docs are saved to",pathes) // the rest of your logic });
where photo and cv are single files and docs is a multiple file in HTML single files look like the following
<input type='file' name='cv'>
<input type='file' name='photo'>
and multiple files look like the following
<input type='file' multiple name='docs'>