How to upload files - sails.js

I can upload images and pdf, but I can not upload document files (.doc.pptx.odt ...)

when loading documents (.doc.pptx.odt ...) only XML files are downloaded as .ZIP. What can I do?

I use: fill files to upload files

upload: function (req, res) {
  req
    .file('avatar')
    .upload({
      maxBytes: 10000000
    }, function whenDone(err, uploadedFiles) {
      if (err) {
        return res.negotiate(err);
      }

      // Generate a unique URL where the avatar can be downloaded.
      avatarUrl = require('util').format('%s/user/avatar/%s', sails.getBaseUrl(), req.session.User.id_user),
      // Grab the first file and use it `fd` (file descriptor)
      avatarFd = uploadedFiles[0].fd

      var SkipperDisk = require('skipper-disk');
      var fileAdapter = SkipperDisk(/* optional opts */);

      // Stream the file down
      fileAdapter.read(avatarFd).on('error', function (err){
        return res.serverError(err);
      }).pipe(res);

  });
},
+4
source share
3 answers

I use this on Windows and it has no problem.

upload  : function (req, res) {
  req.file('avatar').upload({
    maxBytes: 10000000
  }, function whenDone(err, uploadedFiles) {
    if (err) {
      return res.negotiate(err);
    }

    var avatarFd = uploadedFiles[0].fd;

    res.ok(avatarFd);

  });
},
download: function (req, res) {
  var location = req.param('fd');
  var SkipperDisk = require('skipper-disk');
  var fileAdapter = SkipperDisk(/* optional opts */);
  fileAdapter.read(location).on('error', function (err) {
    return res.serverError(err);
  }).pipe(res);
}

First download it using /somecontroller/uploadwith multipart/form-data, it will return the location fd. And download it using this URL

http://localhost:1337/somecontroller/download?fd=[fd-from-upload-return]

The controller name and host name depend on the configuration of your application.

fd , prodtion , , .tmp/uploads/.

+3

, , Sails.js, Skipper. , .

GUID , - . , ".docx", , .docx - zips. ( , , ?)

- saveAs. . :.

req.file('file_upload').upload({saveAs: Guid.raw()+'.docx'}, handleUploadCompletion);

, .

+2

, HTTP-:

  • Content-disposition: ,
    • . msgstr "".
  • : MIME
    • . "/vnd.oasis.opendocument.text"

res.attachment:

res.attachment(avatarFd);
+2

Source: https://habr.com/ru/post/1568658/


All Articles