使用multer / javascript在express.js上传和保存照片

时间:2016-12-19 17:20:22

标签: javascript express upload photo multer

启动项目并上传照片后,我的目标文件夹保持空白。是否有人知道此代码可能存在什么问题?

这是代码:

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 个答案:

没有答案