只读取Sails Js上传的文件。

问题描述 投票:1回答:2

我想上传一个文件,作为一个缓冲区,用utf-8编码解析为字符串subsract该字符串的一部分,仅此而已。

我不想把它保存在磁盘上,因为目前它与 skipper一起航行。

https:/github.combalderdashyskipper)。

node.js sails.js
2个回答
1
投票

我相信你可以通过上传到一个临时文件,然后从那里读取。例如

// 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);
    });
});

0
投票

我知道已经太晚了,但我还是想把这些方法分享给其他人。

你可以用 .reservoir() 方法中 帆钩-上传 来实现这一目标。

例。

const info = await sails.reservoir(uploadedFile, {encoding:'utf8'});

还有... info 应该是。

[
  {
    contentBytes: 'data contents...',
    name: 'filename.txt',
    type: 'text/plain'
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.