简体   繁体   English

如何上传文件 - sails.js

[英]How to upload files - sails.js

I can download images and pdf, but I can't download documents files (.doc .pptx .odt ...) 我可以下载图像和pdf,但我无法下载文件文件(.doc .pptx .odt ...)

when download Documents(.doc .pptx .odt ...) are downloaded as .ZIP only XML files. 下载文档(.doc .pptx .odt ...)时,只下载.ZIP XML文件。 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 use this at Windows and it have no problems. 我在Windows上使用它,它没有问题。

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. 首先使用/somecontroller/upload with multipart/form-data /somecontroller/upload它,它将返回一个fd位置。 And download it using this url 并使用此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/ . 使用fd完整路径只是一个例子,在生产之后你应该使用你自己的控制器从上传的文件夹中寻址到已经上传的文件,它通常在.tmp/uploads/

So you're using a library that is part of the Sails.js ecosystem known as Skipper . 所以你使用的是一个名为Skipper的Sails.js生态系统的库。 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. 它会将您的文件保存到上传时的随机GUID,直到您选择对其执行操作为止。 My guess is that it's not carrying the ".docx" tag with it when it does so and .docx files are really just zips. 我的猜测是它没有携带“.docx”标签,当它这样做时.docx文件实际上只是拉链。 (Maybe this is a bug you can file?) (也许这是你可以提交的错误?)

My suggestion would be to use the saveAs option. 我的建议是使用saveAs选项。 You can specify a string to use as the save location instead. 您可以指定要用作保存位置的字符串。 ie: 即:

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: 我想您在回复中忘记了一些HTTP标头

  • Content-disposition: To set the name of the file you're sending 内容处理:设置您要发送的文件的名称
    • eg "attachment; filename="ecd1c7c2-6e32-4110-8905-c003a4713bb0.odt"" 例如“attachment; filename =”ecd1c7c2-6e32-4110-8905-c003a4713bb0.odt“”
  • Content-type: The MIME type of the content you're sending 内容类型:您要发送的内容的MIME类型
    • eg "application/vnd.oasis.opendocument.text" 例如“application / vnd.oasis.opendocument.text”

You can set them manually or use the res.attachment method: 您可以手动设置它们或使用res.attachment方法:

res.attachment(avatarFd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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