0

I am using bodyboy https://github.com/mscdex/connect-busboy upload functionality. Basically I do not need to save files, its just user uploads a file and I want to save it into my database.

So to be precise, if anyone can tell me how to console contents of file uploaded

req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
            console.log(fieldname)
            console.log(file)
            console.log(filename)
            console.log('readin file')
        });

Here how can I read the file, or even if I need anymore step?

Luckyy
  • 1,021
  • 4
  • 15
  • 29

1 Answers1

0

Actually connect-busboy is a connect middleware for busboy.

You can use following sample code ,

    req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
          console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
          file.on('data', function(data) {
            console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
          });
          file.on('end', function() {
            console.log('File [' + fieldname + '] Finished');
          });
    });

If you want to read your stream then you need to play with your stream or please try to use following way

   req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
               var buffer = '';
               console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
               file.on('data', function(data) {
                  buffer += data;
                 console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
               });
               file.on('end', function() {
                 console.log('File [' + fieldname + '] Finished');
                 var val = JSON.parse(buffer);
                // use `val` here ...
               }).setEncoding('utf8');
         });

And make sure you have used proper headers in your request otherwise please check sample test case code to here.

Hopes this will help you !

Santosh Shinde
  • 6,045
  • 7
  • 44
  • 68
  • Thanks for your answer Santosh but this is not what I was looking for. I am upload a json file and want to read its JSON content once upload is finished. – Luckyy Nov 16 '17 at 07:03