简体   繁体   中英

Sails Js Read uploaded file content

I want get content from my uploaded file. Uploaded file is just text file and i want read this file line by line

req.file("model").upload(function (err, uploadedFiles){
  if (err) return res.serverError(err);

  return res.json({
    message: uploadedFiles.length + ' file(s) uploaded successfully!',
    files: uploadedFiles,
    content: uploadedFiles[0] // I want to get my uploaded file content
                              // Stream or buffer
  });

});

You can get the file descriptor from uploadedFiles[0].fd

Use it to read / stream the file.

fs.readFile(uploadedFiles[0].fd, 'utf8', function (err,data) {
        return res.json(200, {message: 'Ok', data: data});
})

First install Local filesystem streaming binary adapter for Sails.js / Waterline

npm install sails-local-fs

in your code create fs instance

var fs = require('fs');

And now use readFile() method to get content of your file

fs.readFile(files[0].fd, 'utf8', function (err,data) {
    return res.json(200, {message: 'Ok', data: data});
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM