node.js 使用 multer 上传不工作

node.js upload with multer not working

我正在尝试使用 node.js 和 multer 中间件实现文件上传,但它似乎不起作用。这是我的代码:

var express = require('express');
var multer = require('multer');
var done = false;
var app = express();

app.use(multer( {dest:'./uploads/',
            onFileUploadStart : function(file){
                console.log('File recieved:');
                console.log(file);
            },
             onFileUploadData:function (file,data){
                console.log('Data recieved');
            },
             onParseEnd: function(req,next){
                next();
             }
            }));

app.use(express.static(__dirname+"/public"));

app.post('/upload',require(__dirname+'/upload.js').upload);

app.listen(3000);

我的表单如下所示:

<html>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name ="file">
    <input type="submit" value="Upload selected file to server">
</form>    
</body>
</html>

而 upload.js 看起来像这样:

exports.upload = function (req,res)
{
   console.dir(req.files);
};

我认为问题在于我提交的表单在 Content-Type header 中使用 "application/x-www-form-urlencoded" 而不是 "multipart/form-data",因为这是我使用时出现的内容Fiddler 来监视请求,但我不知道为什么。任何人都可以阐明这一点吗?

我看得出你做的一切都是对的。我以前用过 multer,你可以查看我的工作实现 here. In this EJS file i have an image upload functionality 然后我写了一些逻辑来将文件存储到其他位置。

确保您有合适的路由器,例如您可以使用 router.post(..)

exports.upload= function(req,res){
    // get the temporary location of the file
    var tmp_path = req.files.file.path;
    // set where the file should actually exists - in this case it is in the "images" directory
    var target_path = '/..provide path to store photos../' + req.files.file.name;
    // move the file from the temporary location to the intended location
    fs.rename(tmp_path, target_path, function(err) {
        if (err) throw err;
        // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
        fs.unlink(tmp_path, function() {
            if (err) {
                throw err;
            }else{
              //response logic ...
             };
            });
        });
  };

我通过在我的 html 中向我的标签添加一个接受属性来让它工作。我不知道为什么在某些示例中没有使用它。

这是我整个表单的代码:

<form action="/upload" method="post" enctype="multipart/form-data"> 

    <input type="file" name ="file" accept="application/x-zip-compressed,image/*"> 

    <input type="submit" value="Upload selected file to server"> 

</form>

最终检查整个项目:https://github.com/tutiplain/quickupload

你可以试试这个。这个对我有用。如果有任何问题,请告诉我

var multer  = require('multer');
var upload = multer({ dest: './uploads' });

router.post('/register',upload.single('profileimage'),function(req,res,next){

    if (req.file) {
        console.log('Uploading File');
        var profileImageOriginlName=req.file.originalname;
        var profileImageName=req.file.name;
        var profileImageMime=req.file.mimetype;
        var profileImagePath=req.file.path;
        var profileImageExt=req.file.extension;
        var profileImageSize=req.file.size;
    }
    else
    {
        var profileImageName='noimage.png';
    }

});