1
<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);
    };

Submit form without enctype="multipart/form-data" is working but I can not get files.

Submit form with enctype="multipart/form-data" is working but I can not get files as well as data.

2
  • were you able to fix d issue. Even im facing the same problem Commented Sep 24, 2015 at 4:22
  • Refer this link for a new way of using multer. stackoverflow.com/questions/32045027/… Commented Oct 3, 2015 at 19:51

2 Answers 2

1

you can try this

<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");
});

Now you can upload upto 8 files at a time, if you want to upload more than eight, just edit var upload = multer({ storage : storage }).array('userPhoto','LIMITHERE');

Sign up to request clarification or add additional context in comments.

Comments

0

@karthik comment, i think you need this as well including after jquery, when you want to use his example:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.