[英]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
您可以从
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 首先为Sails.js / Waterline安装本地文件系统流二进制适配器
npm install sails-local-fs
in your code create fs instance 在您的代码中创建fs实例
var fs = require('fs');
And now use readFile() method to get content of your file 现在使用readFile()方法获取文件内容
fs.readFile(files[0].fd, 'utf8', function (err,data) {
return res.json(200, {message: 'Ok', data: data});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.