Я хочу загрузить файл в качестве буфера с синтаксическим анализом кодировки utf-8, чтобы строка вычитала часть этой строки и все.

Я не хочу сохранять его на диске, поскольку сейчас он плывет со шкипером.

https://github.com/balderdashy/skipper

1
dpineda 24 Янв 2015 в 00:02

2 ответа

Лучший ответ

Я считаю, что вы можете сделать это, загрузив во временный файл, а затем прочитав оттуда. Например.

// 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);
    });
});
1
rmacqueen 22 Апр 2015 в 15:35

Я знаю, что было слишком поздно, но все же хочу поделиться этими методами с другими:

Вы можете использовать метод .reservoir() в sails-hook-uploads, чтобы добиться этого.

Примере:

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

И info должен быть:

[
  {
    contentBytes: 'data contents...',
    name: 'filename.txt',
    type: 'text/plain'
  }
]
1
Chenglong Ma 8 Май 2020 в 04:49