You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The from file input field has the "name" attribute set to "upfile". We rely on this in testing.
When I submit something, I will receive the file name and size in bytes within the JSON response
To handle the file uploading you should use the multer npm package.
📷 Screenshots
.
📶 Technologies
Node v14 javaScript runtime built on Chrome's V8 JavaScript engine
Express v5 Fast, unopinionated, minimalist web framework for Node.js
multer v1 node.js middleware for handling multipart/form-data, mainly used for uploading files
Cors v2 node.js package for providing Connect/Express middleware that can be used to enable CORS with various options.
💾 Setup
Run node server.js for a dev server. Navigate to http://localhost:8080/.
The app will not automatically reload if you change any of the source files.
💻 Code Examples
extract from server.js to upload file and produce a json object with file details (or error message if it does not work)
app.post('/api/fileanalyse',upload.single('upfile'),(req,res,next)=>{constfileSize=req.file&&req.file.size;console.log(typeoffileSize);//should return 'number'res.json(typeoffileSize=='undefined'
? {error: 'sorry, but there is a file error'}
: {name: req.file.originalname,type: req.file.mimetype,size: req.file.size+' bytes',});});
🆒 Features
multer used to upload files - uploaded files stored in a folder named 'uploads'. If this folder does not exist then it is created.