本文介绍了Multer-如果表单验证失败,则停止上载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我将表单与文件上载和常规表单字段混合在一起,并且在将此表单发送到Nodejs服务器时,我正在验证表单,但问题是我不知道如何停止上传文件(例如(如果)表单之一)字段为空.例如:

i have mixed form with file upload and normal form fields, and while sending this form to Nodejs server i am validating form but problem is that i do not know how to stop uploading the file if (for example) one of the form field is empty.so for example:

if(name.trim().length < 1){
   //stop uploading of file
   return res.status(409).send({
     message: 'provide name'
   })
 }

我该怎么做(Multer& ExpressJS)?

how can i do that (Multer & ExpressJS)?

推荐答案

在我的情况下,我正在使用以下代码(node& express).

I was using following code in my case (node & express).

从routes.js文件中,我正在调用此 insertRating 方法.像下面一样

From routes.js file I am calling this insertRating method. like below

//routes.js
router.post('/common/insertrating',RatingService.insertRating);

//controller class
// storage & upload are configurations

var storage = multer.diskStorage({
destination: function (req, file, cb) {

    var dateObj = new Date();
    var month = dateObj.getUTCMonth() + 1; //months from 1-12

    var year = dateObj.getUTCFullYear();
    console.log("month and yeare are " + month + " year " + year);
    var quarterMonth = "";
    var quarterYear = "";

    var dir_path = '../../uploads/' + year + '/' + quarterMonth;

    mkdirp(dir_path, function (err) {
        if (err) {
            console.log("error is cominggg insidee");
        }
        else {
            console.log("folder is createtd ")
        }
        cb(null, '../../uploads/' + year + '/' + quarterMonth)
    })

    console.log("incomingggg to destination");

},
filename: function (req, file, cb) {

    console.log("incoming to filename")
    cb(null, Date.now() + "_" + file.originalname);
},

});

var upload = multer({
storage: storage,
limits: {
    fileSize: 1048576 * 5
},
fileFilter: function (req, file, callback) {
    var ext = path.extname(file.originalname);
    ext = ext.toLowerCase();
    console.log("ext isss " + ext);

    if (ext !== '.png' && ext !== '.jpg' && ext !== '.jpeg' && ext !== '.pdf' && ext !== '.txt'
        && ext !== '.doc' && ext !== '.docx' && ext !== '.xlsx' && ext !== '.xls'
    ) {
        return callback(new Error('Only specific extensions are allowed'))
    }
    callback(null, true)
}

}).array('files', 5);


 // calling below insertRating method from routes js file...

exports.insertRating = async function (req, res) {
        let quarterData = req.query.quarterData;
        quarterData = JSON.parse(quarterData);

        if (quarterData != null) { // here you can check your custom condition like name.trim().length
               req.files = []; // not required
               upload(req, res, async function (err) { // calling upload
                            if (err) {

                                res.send("error is cominggg")
                                return;
                            } else {
                                res.send("file is uploaded");
                      }

                            //
               });

        }
       else {
               res.send("filed must not be empty");
       }
}

这篇关于Multer-如果表单验证失败,则停止上载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:15