Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to Multer buffer object

I want to use the uploaded 'file.txt' , using multer and line-by-line modules. After uploading the file with multer, I tried to check on the data, I got a buffer object, and I can't figure out how to access the data and work on it with the line-by-line module. Here is my code :

var express = require("express"),
fs = require('fs'),
app = express(),
bodyParser = require("body-parser"),
multer  = require('multer'),
upload = multer({ 
 dest: 'uploads/',
 inMemory:true,
 onFileUploadData: function (file, data) {
    console.log(data.length + ' of ' + file.fieldname + ' arrived')
    } 
}),
LineByLineReader = require('line-by-line'),
path = require('path'),
ejs = require("ejs"),
Excel= require("exceljs");
app.engine('html', ejs.renderFile); 
app.set('view engine', 'html');
app.use("/static", express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));

app.post("/GffData", upload.single('file'), function (req, res) {
    console.log("this is the request : ", req.file);

    fs.readFile(req.file.path, function (err, data) {
        if (err) throw err;
        // data will contain your file contents
        console.log("the data is : ",data)
        generateObjectFromGff(data); //this is the function that will use line by line module
    });

    res.send("done")
})

When I see the data on my console , i get a buffer :

<Buffer 4c 6d 6a 46 2e 30 31 09 54 72 69 54 72 79 70 44 42 09 43 44 53 09 33 37 30 34 09 34 37 30 32 09 2e 09 2d 09 30 09 22 49 44 3d 63 64 73 5f 4c 6d 6a 46 ... >

And the line-by-line module can"t proceed on this. Can you help me how to figure it out?

like image 274
RachaBella Avatar asked Nov 08 '22 17:11

RachaBella


1 Answers

If your data variable is Buffer object you can easily convert it to string by adding empty string: data + ''. If you want access your text line by line you can convert it to array of lines:

(data + '').split('\n')
like image 184
Eugene Mala Avatar answered Nov 15 '22 06:11

Eugene Mala