Anders Östman
Anders Östman

Reputation: 3832

File uploading with Connect

Im trying to upload a file and examine it with a this simple code.

var connect = require("connect")
    , http = require('http');

var app = connect()
    .use(connect.logger('dev'))
    .use(connect.static('static'))
    .use(connect.bodyParser())
    .use(function(req, res, next){
        if ('POST' == req.method) {
            console.log(req.body);
            console.log(req.body.file);
            res.writeHead(200);
            res.end('Uploaded');
        } else {
            next();
        }
    });

connect.createServer(app).listen(3000);

In the static folder i have this index.html:

 <form action="/" method="POST" enctype="multipart/form-data">
     <input type="text" name="texten">
     <input type="file" name="displayImage">

     <button>Send file!</button>
 </form>

In the browser I write some text in the text-field, choose a simple .txt file for the file-input. Then i press submit.

In the console the content of the textfield is outputted as expected, but the file or information about it is nowhere to be found.

Q: Where in req.body are the file located, and how to i access the information about it?

Upvotes: 0

Views: 35

Answers (2)

adamK
adamK

Reputation: 3889

multipart submissions required for your file upload are not handled by bodyparser. You will need some other middleware such as connect-busboy or connect-multiparty.

You could then access the uploaded file from the req.files object.

Upvotes: 1

Edgar
Edgar

Reputation: 113

You should use file system core module to write the content of the body to file.

Upvotes: 0

Related Questions