How to upload files - sails.js

不羁岁月 提交于 2019-12-10 17:15:18

问题


I can download images and pdf, but I can't download documents files (.doc .pptx .odt ...)

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

I'm using : fill out upload file docs

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's `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);

  });
},

回答1:


I use this at Windows and it have no problems.

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 upload it using /somecontroller/upload with multipart/form-data, it will return an fd location. And download it using this url

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

Controller name and host name is depends on your application configuration.

Using fd full path is just for an example, in prodtion later you should use your own controller to address from uploaded folder to return file that has been uploaded, it's usually on .tmp/uploads/.




回答2:


So you're using a library that is part of the Sails.js ecosystem known as Skipper. It has some documentation that may be helpful to you.

It saves your file to a random GUID on upload until you choose to do something with it. My guess is that it's not carrying the ".docx" tag with it when it does so and .docx files are really just zips. (Maybe this is a bug you can file?)

My suggestion would be to use the saveAs option. You can specify a string to use as the save location instead. i.e.:

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

There is also the ability to pass a function so handle multi-file uploads depending on the stream.




回答3:


I think you're forgetting some HTTP headers in your response:

  • Content-disposition: To set the name of the file you're sending
    • e.g. "attachment; filename="ecd1c7c2-6e32-4110-8905-c003a4713bb0.odt""
  • Content-type: The MIME type of the content you're sending
    • e.g. "application/vnd.oasis.opendocument.text"

You can set them manually or use the res.attachment method:

res.attachment(avatarFd);


来源:https://stackoverflow.com/questions/27608304/how-to-upload-files-sails-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!