如何使用sails.js 中的新控制器操作格式上传文件 [英] How to upload a file using the new controller actions format in sails.js

查看:34
本文介绍了如何使用sails.js 中的新控制器操作格式上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我如何使用sails.js的(输入,存在)格式将文件上传到控制器动作,这是我的尝试:

Someone know how I could upload a file to a controller action using the (inputs, exists) format of sails.js, this is my try:

  module.exports = {


  friendlyName: 'Upload file recording',


  description: 'Upload a recording to AWS',

  inputs: {
    name: {
      required: true,
      type: 'string'
    },
    mimeType: {
      required: true,
      type: 'string'
    },
    recording: {
      type: 'ref'
    }
  },

  exits: {

    noRecordings: {
      responseType: 'no recordings',
      description: `You don't have any recording for this event`,
    },
    serverError: {
      responseType: 'server error',
      description: `Failed to upload the file`,
    }

  },


  fn: async function (inputs, exits) {

    //  fixme    
    inputs.file('recording').upload({
      adapter: require('skipper-s3'),
      key: process.env.AWS_S3_KEY,
      secret: process.env.AWS_S3_SECRET,
      bucket: process.env.AWS_S3_BUCKETNAME
    }, function (err, filesUploaded) {
      if (err) return exits.serverError(err);
      return exits.success({
        files: filesUploaded,
        textParams: req.allParams()
      });
    });

  }
};

上传文件,因为inputs.file 不是函数.所以基本上问题是,我如何在这里传递文件.

The upload files because the inputs.file is not a function. So basically the question is, how can I pass a file here.

感谢任何帮助.

推荐答案

所以终于找到了解决方案,我只需要使用:

So finally found the solution to that, I just need to use:

files: ['recording'],

除了:

inputs:
 recording: {
      example: '===',
      required: true
 },

然后在动作函数内部:

inputs.recording.upload({
      adapter: require('skipper-s3'),
      key: process.env.AWS_S3_KEY,
      secret: process.env.AWS_S3_SECRET,
      bucket: process.env.AWS_S3_BUCKETNAME
    }, function (err, filesUploaded) {
      if (err) return exits.serverError(err);
      console.log(filesUploaded);
      return exits.success(newFileRecording);
    });

这篇关于如何使用sails.js 中的新控制器操作格式上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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