如何修复节点中的“ req.files打印空问题”并表达

时间:2019-01-04 06:57:12

标签: express

在节点中通过express-fileupload表达图像上传问题

我有app.js文件,路由器文件和视图文件。当我上传图像和控制台(req.files)时,它为null。我正在使用express-fileupload软件包管理器。

app.js

const express   = require('express');
const path      = require('path');
var bodyParser = require('body-parser');
var session = require('express-session');
var expressValidator = require('express-validator');
var fileUpload = require('express-fileupload');

// Body Parser middleware
app.use(bodyParser.urlencoded({extended: false}));
// parse application/json
app.use(bodyParser.json());

// Express Fileupload middleware
app.use(fileUpload());

// Set Routes

var adminProducts   = require('./routes/admin_products.js');
app.use('/admin/products', adminProducts);

var port = 3000;
app.listen(port, ()=>{
    console.log('Server is listening on port ' + port);
});

router / admin_product.js

router.post('/add-product', function (req, res) {
   console.log(req.files); // Prints null
});

add-product.ejs

<form method="post" action="/admin/products/add-product" enctype="multipart/form-data">

    <div class="form-group">
        <label for="">Image</label>
        <input type="file" class="form-control" name="image" id="img">
        <img src="#" id="imgPreview" alt="">
    </div>

    <button class="btn btn-default">Submit</button>
</form>

1 个答案:

答案 0 :(得分:0)

在app.js文件中,我们必须提供要保存图像的文件夹的路径-

f