简体   繁体   中英

Only read a uploaded file with Sails Js

I want to upload a file as a buffer with utf-8 encoding parse to string substract a part of that string and thats it.

I don't want to save it on disk as currently does it sails with skipper.

https://github.com/balderdashy/skipper

I believe you can do this by uploading to a temp file and then reading from there. Eg

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

I know it has been too late, but I still want to share these methods for others:

You can use .reservoir() method in sails-hook-uploads to achieve this.

Example:

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

And the info should be:

[
  {
    contentBytes: 'data contents...',
    name: 'filename.txt',
    type: 'text/plain'
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM