最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to upload files - sails.js - Stack Overflow

programmeradmin1浏览0评论

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

  });
},

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

  });
},
Share Improve this question edited Jul 18, 2016 at 9:05 Cris69 6001 gold badge16 silver badges42 bronze badges asked Dec 22, 2014 at 18:21 DanieleduDanieledu 3911 gold badge4 silver badges22 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4 +200

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/.

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.

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);
发布评论

评论列表(0)

  1. 暂无评论