node.js上传,multer无法正常工作

时间:2015-01-11 20:55:41

标签: node.js file-upload express multer

我正在尝试使用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);
};

我认为问题在于我的表单是以&#34; application / x-www-form-urlencoded&#34;提交的。在Content-Type标题中而不是&#34; multipart / form-data&#34;,因为这是我使用Fiddler监视请求时出现的内容,但我不知道为什么。任何人都可以对此有所了解吗?

3 个答案:

答案 0 :(得分:1)

我通过在html中为我的标记添加一个accept属性来实现它。我不知道为什么在某些例子中没有使用它。

这是我整个表单的代码:

<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

答案 1 :(得分:0)

我可以看到你正在做的一切正确。我在某个时候使用了multer,你可以查看我的工作实现here。在这个EJS文件中,我有一个图像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 ...
             };
            });
        });
  };

答案 2 :(得分:0)

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

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';
    }

});