1

I want to upload file using node.js , being new to it a tried to check if the file is being send to server. html

  <html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="img" method="POST" 
      enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

js

var express  = require("express");
var app=express();
var http=require("http").Server(app);


app.get("/",function(req,res){
    res.end("hello")
});

app.get("/upload",function(req,res){
    res.sendFile(__dirname + "/form.html")
})
app.post("/img",function(req,res){
    if(req.files){
     console.log(req.files.file.name);
    }
    else{
        console.log("ee")
    }


});
http.listen(3000,function(){
    console.log("listening on 3000")
})

When i upload something , it throws error

Cannot read files of undefined

Being new to back end i have no idea why its happening , why doesnt the server recieve the file?

1
  • what line throws this error? Commented Feb 23, 2016 at 20:34

2 Answers 2

2

You need to app.use() a fileparser. For example, you could use connect-busboy. You can get more information about options and usage at above link; a simple setup would be somehting like this:

var busboy = require('connect-busboy');
app.use(busboy());

app.post("/img",function(req,res){
        req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
            // ...
        });
        req.busboy.on('field', function (key, value, keyTruncated, valueTruncated) {
            // ...
        });
        req.pipe(req.busboy);
        // etc ...
});
Sign up to request clarification or add additional context in comments.

6 Comments

isnt there build in / native modile for fileparser , or do i have to use external modules for that? I am begginer , at first i would like to know it without using external modules
No, there isn't. Your only alternative is coding it by yourself. @J.dd
Im quite interessted in it. I know i will propably use some module , but where can i read about that - about stuff behind creating own fileparser? I fail to find anything
I think a good starting point would be reading the source code of an existing fileparser, like posted above. Then you'll also find the terminology to do google searches on parts of it
@J.dd There is a complete middleware for express for parsing uploaded files. Check my response below. :)
|
0

As stated in the above answer, you must use a body parser for multipart bodies, but the better solution is to use the express middleware multer which lets you to use req.files just like in your OP.

Also, multer is built on top of busboy which is the fastest multipart body parser for node.js

With multer:

var express = require('express'),
    multer = require("multer"),
    app = express();

app.use(multer({
    dest: path.resolve(__root + path.sep + config.get("localFolders").rawImages),
    limits: {
         files: 2
    }
}));

// handle file upload
app.post("/img", function (req, res, next) {
    var image = req.files.image;
    // do something with image;
    console.log(image.name);
});

Have a look on the documentation for multer within the link provided above. Good luck. :)

Comments

Your Answer

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