node.js多文件上传无法正常工作

时间:2015-01-16 06:28:00

标签: node.js express multer

<form action="http://localhost:3000/examples" method="post" enctype="multipart/form-data" accept="application/json">
    <input type="text" name ="name">
    <input type="text" name ="codedescription">
    <input type="file" name ="file">
    <input type="submit" value="Upload selected file to server">
</form>

var multer  = require('multer');
    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.route('/examples').post(users.requiresLogin, examples.create);

exports.create = function(req, res) {
        console.log("req.files"+req.files);
        console.log("req.name"+req.body.name);
        console.log("req.codedescription"+req.body.codedescription);
    };

提交不带enctype的表格=&#34; multipart / form-data&#34;正在工作,但我无法获取文件。

使用enctype =&#34; multipart / form-data&#34;提交表单正在工作,但我无法获取文件和数据。

2 个答案:

答案 0 :(得分:1)

你可以试试这个

<html>
<head>
<title>FileUpload</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<form id       =  "uploadForm"
     enctype   =  "multipart/form-data"
     action    =  "http://localhost:3000/api/photo"
     method    =  "post"
>
<input type="file" name="userPhoto" multiple />
<input type="submit" value="Upload Image" name="submit" id="btnUpload">
<span id="spnStatus" />
</form>

<script>
        $(document).ready(function(){
            $('#btnUpload').click(function(){
                $('#spnStatus').empty().text("File is Uploading");
                $(this).ajaxSubmit({
                 error : function(xhr){
                    status('Error : '+xhr.status);
                    }

                 success : function(response){
                    $('#spnStatus').empty().text(xhr);
                    }
                });
            });

        });
    </script>
</body>
</html>

NodeJS Express

var express =   require("../node_modules/express");
var multer  =   require('../node_modules/multer');
var bodyParser =    require("../node_modules/body-parser");
var app =   express();
app.use(bodyParser.json());


var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './uploads');
  },
  filename: function (req, file, callback) {
    callback(null, file.fieldname + '-' + Date.now());
  }
});
var upload = multer({ storage : storage }).array('userPhoto',8);

app.get('/',function(req,res){
      res.sendFile(__dirname + "/fileUpload.html");
});

app.post('/api/photo',function(req,res){

          upload(req,res,function(err) {
            if(err) {
              console.log(err);
                return res.end("Error uploading file.");
            }
            res.end("File is uploaded");
         });
});

app.listen(3001,function(){
    console.log("Working on port 3001");
});

现在,您一次最多可以上传8个文件,如果您要上传的数量超过8个,只需修改var upload = multer({ storage : storage }).array('userPhoto','LIMITHERE');

答案 1 :(得分:0)

@karthik评论,我认为你需要这个,包括在jquery之后,当你想要使用他的例子时:

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js"></script>