如何从缓冲区中提取文件内容?
How to extract file contents from a Buffer?
我正在探索 Node.js,在创建使用 multer
模块将小文件上传到内存的功能时遇到了一个问题。
我正在使用 multer
的内置 MemoryStorage
选项。文件上传成功,文件元数据可以在 ExpressJS
的 req
对象中访问。
现在我希望能够从缓冲区中读取文件内容,因为该文件将采用 .csv 格式,稍后将转换为 JSON。我需要文件的字符串内容来执行转换。
这是我的路由器处理程序:
const memStorage = multer.memoryStorage();
const memUpload = multer({
storage: memStorage,
limits: { fileSize: 30 * 1024 * 2014, files: 1 }
});
router.post(
'/tables/csv',
memUpload.single('file'),
(req, res) => {
const file = req.file;
let buffer = fs.readFileSync(file.buffer);
console.log(buffer);
}
);
.csv 文件使用:
Risk Category,Risk ID,Risk Value
Some,Some,Some
Some,Some,Some
Some,Some,Some
控制台输出:
Error: ENOENT: no such file or directory, open 'Risk Category,Risk ID,Risk Value
Some,Some,Some
Some,Some,Some
Some,Some,Some'
at Object.openSync (fs.js:436:3)
at Object.readFileSync (fs.js:341:35)
at router.post (C:\client_projects\tt\sarcs-hotline\router\index.js:1039:21)
at Layer.handle [as handle_request] (C:\client_projects\tt\sarcs-hotline\node_modules\express\lib\router\layer.js:95:5)
at next (C:\client_projects\tt\sarcs-hotline\node_modules\express\lib\router\route.js:137:13)
at Array.<anonymous> (C:\client_projects\tt\sarcs-hotline\node_modules\multer\lib\make-middleware.js:53:37)
at listener (C:\client_projects\tt\sarcs-hotline\node_modules\on-finished\index.js:169:15)
at onFinish (C:\client_projects\tt\sarcs-hotline\node_modules\on-finished\index.js:100:5)
at callback (C:\client_projects\tt\sarcs-hotline\node_modules\ee-first\index.js:55:10)
at IncomingMessage.onevent (C:\client_projects\tt\sarcs-hotline\node_modules\ee-first\index.js:93:5)
at IncomingMessage.emit (events.js:182:13)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
您可以在调用 fs.readFileSync
获取字符串时指定您的编码:
fs.readFileSync(file.buffer, { encoding: 'utf8' });
或者您不需要 readFileSync
。你可以像
一样调用buffer.toString(encoding)
file.buffer.toString('utf8');
我正在探索 Node.js,在创建使用 multer
模块将小文件上传到内存的功能时遇到了一个问题。
我正在使用 multer
的内置 MemoryStorage
选项。文件上传成功,文件元数据可以在 ExpressJS
的 req
对象中访问。
现在我希望能够从缓冲区中读取文件内容,因为该文件将采用 .csv 格式,稍后将转换为 JSON。我需要文件的字符串内容来执行转换。
这是我的路由器处理程序:
const memStorage = multer.memoryStorage();
const memUpload = multer({
storage: memStorage,
limits: { fileSize: 30 * 1024 * 2014, files: 1 }
});
router.post(
'/tables/csv',
memUpload.single('file'),
(req, res) => {
const file = req.file;
let buffer = fs.readFileSync(file.buffer);
console.log(buffer);
}
);
.csv 文件使用:
Risk Category,Risk ID,Risk Value
Some,Some,Some
Some,Some,Some
Some,Some,Some
控制台输出:
Error: ENOENT: no such file or directory, open 'Risk Category,Risk ID,Risk Value
Some,Some,Some
Some,Some,Some
Some,Some,Some'
at Object.openSync (fs.js:436:3)
at Object.readFileSync (fs.js:341:35)
at router.post (C:\client_projects\tt\sarcs-hotline\router\index.js:1039:21)
at Layer.handle [as handle_request] (C:\client_projects\tt\sarcs-hotline\node_modules\express\lib\router\layer.js:95:5)
at next (C:\client_projects\tt\sarcs-hotline\node_modules\express\lib\router\route.js:137:13)
at Array.<anonymous> (C:\client_projects\tt\sarcs-hotline\node_modules\multer\lib\make-middleware.js:53:37)
at listener (C:\client_projects\tt\sarcs-hotline\node_modules\on-finished\index.js:169:15)
at onFinish (C:\client_projects\tt\sarcs-hotline\node_modules\on-finished\index.js:100:5)
at callback (C:\client_projects\tt\sarcs-hotline\node_modules\ee-first\index.js:55:10)
at IncomingMessage.onevent (C:\client_projects\tt\sarcs-hotline\node_modules\ee-first\index.js:93:5)
at IncomingMessage.emit (events.js:182:13)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
您可以在调用 fs.readFileSync
获取字符串时指定您的编码:
fs.readFileSync(file.buffer, { encoding: 'utf8' });
或者您不需要 readFileSync
。你可以像
buffer.toString(encoding)
file.buffer.toString('utf8');