0

我正在尝试使用 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 标头中使用“application/x-www-form-urlencoded”而不是“multipart/form-data”提交的,因为这是我使用 Fiddler 监控时出现的内容请求,但我不知道为什么。任何人都可以对此有所了解吗?

4

3 回答 3

1

我通过在我的 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

于 2015-01-16T02:25:01.997 回答
0

我可以看到你做的一切都是正确的。我曾经使用过 multer,你可以在这里查看我的工作实现。在这个EJS文件中,我有一个图像上传功能,然后我编写了一些逻辑来将文件存储到其他位置。

确保你有合适的路由器,例如你可以使用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 ...
             };
            });
        });
  };
于 2015-01-12T10:06:08.410 回答
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';
    }

});
于 2016-05-12T17:13:55.883 回答