将照片上传到Node.js的文件夹中

时间:2018-07-26 10:36:16

标签: node.js

我想使用Node.js将图片上传到文件夹中,但我不知道该怎么做 这是我在ImageDao中插入的图片

exports.insert = function(data, callback){
    console.log("in imagesDao insert");
    var query = " insert into " + tableName + " (url,ajoute_par)";
        query = query + " values(?,?);";
    var values = [data.url , data.ajoute_par];
     // var values = [encodeURIComponent(data.url) , data.ajoute_par];
    database.execute(query, values, function(){
        callback();
    });
}

这是我的图像控制器

// insert
exports.write = function(request, response){
    console.log("in images write");
    // Get the data.
    var postData = "";
    request.on('data', function(data){  // request.on is a listener. Call when data can be read
        postData = postData + data;
    });
    request.on('end', function(){       // Called when data has been read
        var dataObj = JSON.parse(postData);
        dao.insert(dataObj, function(){
            send(response, '{"write result" : "Inserted successfuly"}');
        });

    });
}

1 个答案:

答案 0 :(得分:0)

要上传文件,可以使用nodejs的multer模块。 https://github.com/expressjs/multer

    images_storage: function () {
        return multer.diskStorage({
            destination: function (req, file, cb) {
                mkdirp(Config.upload_images_path, function (err) {
                });
                cb(null, Config.upload_images_path)
            }
            ,
            filename: function (req, file, cb) {
                var getFileExt = function (fileName) {
                    var fileExt = fileName.split(".");
                    if (fileExt.length === 1 || (fileExt[0] === "" && fileExt.length === 2)) {
                        return "";
                    }
                    return fileExt.pop();
                }
                cb(null, Date.now() + '.' + getFileExt(file.originalname))
            }
        });
    },



// Image uploading
const fs = require('fs');
const multer = require('multer');
const Uploads = multer({
    storage: utility.images_storage(),
    fileFilter: function (req, file, cb) {
        if (Config.image_format_arr.indexOf(file.mimetype))
            cb(null, true);
        else
            cb(null, false);
    }
});


//And in your route you can use the upload function

router.post('/upload-logo', Uploads.single('school_logo'), function (req, res, next) {
    var school_id = req.body.school_id;
    var result = {flag: false, message: "Error Occurred! in saving school logo."};
    console.log("REQUEST FILES " + req.file);
    // Save School Logo
    if (typeof req.file != 'undefined' && req.file.size > 0) {
        School.findByIdAndUpdate(school_id, {
            $set: {
                school_logo: req.file.filename
            }
        }, {'new': true}, function (err, school_details) {
            console.log("school_details  " + school_details);
            if (!err && school_details) {
                result.flag = true;
                result.message = "School logo has been successfully updated";
                result.path = '/uploads/images/' + school_details.school_logo;
                //req.session.school_details = school_details;
                utility.upgradeSchoolLogoSessionValue(school_details, false, function (updated_school_details) {
                    console.log("BEFOR SESSION IS UPDATED" + JSON.stringify(req.session.school_details));
                    req.session.school_details = updated_school_details;
                    console.log("SESSION IS UPDATED" + JSON.stringify(req.session.school_details));
                });


                console.log("FILE NAME IS THIS " + req.file.filename);
            }
            res.json(JSON.stringify(result));
        });
    }
    else {
        res.json(JSON.stringify(result));
    }
});