How to upload a file using the new controller actions format in sails.js

别来无恙 提交于 2019-12-23 05:12:29

问题


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

  }
};

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

Any help is appreciated.


回答1:


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

files: ['recording'],

in addition to:

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

And then inside the action function:

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


来源:https://stackoverflow.com/questions/50477645/how-to-upload-a-file-using-the-new-controller-actions-format-in-sails-js

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