How to upload files with express/file upload?

The module express is a very powerful module, but many often fall short in their documentation. The first thing that every developer must do when working with the express module, is set up the packages. One of these packages is multipart which will be used for uploading files, I assume that you have set up your application to use mongo-haystack and that express-fileupload is installed.

express file upload mv

By Curious CorncrakeCurious Corncrake on Feb 10, 2021
app.post('/upload-avatar', async (req, res) => {
    try {
        if(!req.files) {
            res.send({
                status: false,
                message: 'No file uploaded'
            });
        } else {
            //Use the name of the input field (i.e. "avatar") to retrieve the uploaded file
            let avatar = req.files.avatar;
            
            //Use the mv() method to place the file in upload directory (i.e. "uploads")
            avatar.mv('./uploads/' + avatar.name);

            //send response
            res.send({
                status: true,
                message: 'File is uploaded',
                data: {
                    name: avatar.name,
                    mimetype: avatar.mimetype,
                    size: avatar.size
                }
            });
        }
    } catch (err) {
        res.status(500).send(err);
    }
});

Source: attacomsian.com

Add Comment

0

File upload is an essential component of most web apps, and it is the backbone of File-Transfer HTML5 API. When using File-Transfer API you should make sure that you have a proper server-side implementation in place.

Javascript answers related to "express file upload mv"

View All Javascript queries

Javascript queries related to "express file upload mv"

express file upload mv angular file upload app with django react upload file axios upload a file from react native expo to s3 upload image file in react native using rest api to website angular image upload jquery save upload image reactjs upload zip using fetch upload image in firebase storage react web body parser express Express.js - app.listen vs server.listen express validator typescript express validator express multer react file input reactjs cdn file vue electron read file node js serve pdf file node js store add values in file node js store values in file node js write file node js write read string to file node load file node load string from file node open file node read file node read file line node read file stream node read file sync nodejs write to log file read a file nodejs read file in nodejs using fs read html file node js read json file nodejs read txt file in node js read json file flutter Javascript write to text file send file discord js v12 read file javascript how to use javascript to get full file path javascript add css file exceljs read file example insert json file in python how to code a minecraft json file mod multer save file with extension html bind js file download file javascript javascript download file search string in file node how to make a file and write code in it javascript javascript check if file exists in folder javascript file preview nodemon install locally json file multer rename file how add all json files to one json file in command prompt Could not find a declaration file for module 'react'. disable lint for file ENOENT, no such file or directory download file in react ould not find a declaration file for module 'react-router-dom'. node js serve pdf file axios send file 'nodemon' is not recognized as an internal or external command, operable program or batch file. Could not find a declaration file for module 'react' how to run mocha tests form a file Fs write file download file from url javascript

Browse Other Code Languages

CodeProZone