仅使用 Sails Js 读取上传的文件
Only read a uploaded file with Sails Js
我想上传一个文件作为缓冲区,使用 utf-8 编码解析为字符串减去该字符串的一部分,仅此而已。
我不想将它保存在磁盘上,因为它目前使用船长航行。
我相信您可以通过上传到临时文件然后从那里读取来做到这一点。例如
// Since we don't provide any other 'opts' arguments to the upload
// method, it will upload our file to a temp directory
skipperFile.upload(function (err, uploadedFiles) {
// If no files were uploaded, respond with an error.
if (uploadedFiles.length === 0) {
return res.badRequest('No file was uploaded');
}
// Now that the file is uploaded, get the file descriptor
var fd = uploadedFiles[0].fd;
fs.readFile(fd, 'utf8', function (err, data) {
// Print the contents of the file as a string here
// and do whatever other string processing you want
console.log(data);
});
});
我知道已经晚了,但我还是想把这些方法分享给其他人:
您可以使用 sails-hook-uploads 中的 .reservoir()
方法来实现。
示例:
const info = await sails.reservoir(uploadedFile, {encoding:'utf8'});
info
应该是:
[
{
contentBytes: 'data contents...',
name: 'filename.txt',
type: 'text/plain'
}
]
我想上传一个文件作为缓冲区,使用 utf-8 编码解析为字符串减去该字符串的一部分,仅此而已。
我不想将它保存在磁盘上,因为它目前使用船长航行。
我相信您可以通过上传到临时文件然后从那里读取来做到这一点。例如
// Since we don't provide any other 'opts' arguments to the upload
// method, it will upload our file to a temp directory
skipperFile.upload(function (err, uploadedFiles) {
// If no files were uploaded, respond with an error.
if (uploadedFiles.length === 0) {
return res.badRequest('No file was uploaded');
}
// Now that the file is uploaded, get the file descriptor
var fd = uploadedFiles[0].fd;
fs.readFile(fd, 'utf8', function (err, data) {
// Print the contents of the file as a string here
// and do whatever other string processing you want
console.log(data);
});
});
我知道已经晚了,但我还是想把这些方法分享给其他人:
您可以使用 sails-hook-uploads 中的 .reservoir()
方法来实现。
示例:
const info = await sails.reservoir(uploadedFile, {encoding:'utf8'});
info
应该是:
[
{
contentBytes: 'data contents...',
name: 'filename.txt',
type: 'text/plain'
}
]