Uploading and Saving photo in express.js with multer / javascript

301 Views Asked by At

After starting the project and upload a photo , my destination folder stays empty.Does anybody know what might be the problem with this code?

This is the code :

const express =   require("express");
const multer  =   require('multer');
const app         =   express();
var storage =   multer.diskStorage({
    destination: function (req, file, callback) {
        callback(null, '/public/uploads');
    },
    filename: function (req, file, callback) {
        callback(null, file.fieldname + '-' + Date.now());
    }
});
var upload = multer({ storage : storage});

 app.get('/public/uploads',function(req,res){
        res.sendFile(__dirname + "/public/uploads");
    });

    app.post('/public/uploads',upload.single('userPhoto'),function(req,res){
        console.log(req.files);
        res.send('Successfully uploaded!');
    });

    app.listen(5000,function(){
        console.log("Working on port 5000");
    });

This is the HTML FORM:
<div class="form-group">
                    <div class="col-sm-4 col-sm-offset-4">
                        <form id="uploadForm" method="post" enctype="multipart/form-data" action="/public/uploads">
                            <p><b>Choose Profile Picture:</b></p>
                            <input type="file" name="userPhoto">
                            <input type="submit" value="Upload Image" name="submit">
                        </form>
                    </div>
                </div>
0

There are 0 best solutions below