1

I am trying to make a simple file server able to save a file using express:

var app = express();
app.use(express.bodyParser({uploadDir:'./uploads'}))
app.post('/upload', function(req, res, next) {
    console.log("Uploading File");

    req.on('data', function(raw) {
        console.log('received data: ' + raw);
    });

    req.on('end', function() {
        console.log('File uploaded');
        res.send(200);
    });

});

I am only getting a empty file in "uploads" folder.

What am I missing?

Maybe is a really silly question but I am new with node.js.

Edit: More information...

I am sending this from my client:

POST /upload HTTP/1.1
Connection: Close
Content-Length: 42
Content-Type: multipart/form-data; boundary="MIME_boundary_572B1A2B457D3267"
Cookie: session.id=b268b12e-0c05-11e3-8702-7a7919510927
Host: localhost:8080
Transfer-Encoding: chunked


Client 3 Received Message: E4
--MIME_boundary_572B1A2B457D3267
Content-Disposition: form-data; name="test1.txt"; filename="test1.txt"
Content-Type: application/octet-stream

File1 from client in W7 to send to server.
--MIME_boundary_572B1A2B457D3267--

0

In two chuncks.

Miguel Angel
  • 630
  • 7
  • 18

1 Answers1

1

Regarding to > getting an empty file in "uploads" folder,

You must save the (decoded) request body to a file.

Also note that the client content encoding is multipart. Resembling the multipart content is tricky. You can use bodyParser middleware for that.

Take a look at this answer for more info.

Community
  • 1
  • 1
fardjad
  • 20,031
  • 6
  • 53
  • 68