如何上传文件-sails.js [英] How to upload files - sails.js

查看:44
本文介绍了如何上传文件-sails.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以下载图片和pdf,但我不能下载文档文件(.doc .pptx .odt ...)

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

当下载文档(.doc .pptx .odt ...)作为 .ZIP 仅 XML 文件下载时.我能做什么?

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

我正在使用:填写上传文件文档

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

  });
},

推荐答案

我在 Windows 上使用它,它没有问题.

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

首先使用 /somecontroller/uploadmultipart/form-data 上传它,它会返回一个 fd 位置.并使用此网址下载

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.

使用 fd 完整路径只是一个例子,以后你应该使用你自己的控制器从上传的文件夹中寻址返回已经上传的文件,它通常在 .tmp/uploads/.

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

这篇关于如何上传文件-sails.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆