首页 > 解决方案 > SailsJS action2 多文件上传

问题描述

我正在尝试从sailsjs 1.2.4 上传多个文件

这是我的行动:

module.exports = {


  friendlyName: 'Post',


  description: 'Post something.',

  files: ['mediaFiles'],

  inputs: {
    text : {
      required: true,
      type: 'string',
    },
    
    mediaFiles : {
      description : "Media files",
      example: '===',
      required : false
    }
  },


  exits: {

  },


  fn: async function (inputs) {
       inputs.mediaFiles._files.forEach(file=>{
            console.log(file)
       })
  })
}
}

我得到以下结果作为文件对象:

{ stream:
   PassThrough {
     _readableState:
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: BufferList { head: [Object], tail: [Object], length: 2 },
        length: 43093,
        pipes: null,
        pipesCount: 0,
        flowing: null,
        ended: true,
        endEmitted: false,
        reading: false,
        sync: false,
        needReadable: false,
        emittedReadable: true,
        readableListening: false,
        resumeScheduled: false,
        emitClose: true,
        destroyed: false,
        defaultEncoding: 'utf8',
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events:
      { prefinish: [Function: prefinish],
        drain: [Function],
        end: [Function],
        error: [Array] },
     _eventsCount: 4,
     _maxListeners: undefined,
     _writableState:
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: false,
        needDrain: false,
        ending: true,
        ended: true,
        finished: true,
        destroyed: false,
        decodeStrings: true,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        corked: 0,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: true,
        errorEmitted: false,
        emitClose: true,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: false,
     allowHalfOpen: true,
     _transformState:
      { afterTransform: [Function: bound afterTransform],
        needTransform: false,
        transforming: false,
        writecb: null,
        writechunk: null,
        writeencoding: 'buffer' },
     headers:
      { 'content-disposition':
         'form-data; name="mediaFiles"; filename="bwDzrPkA_400x400.jpg"',
        'content-type': 'image/jpeg' },
     name: 'mediaFiles',
     filename: 'bwDzrPkA_400x400.jpg',
     byteOffset: 378,
     byteCount: 43093,
     field: 'mediaFiles' },
  status: 'bufferingOrWriting' }

我的问题是如何将此文件流写入 /public/media/xyz.png 之类的路径。我曾经使用正常的文件上传,可以使用 this.req.file("name").upload() .. 但不能在操作 2 中使用。我检查了其他答案,但他们正在上传到 s3 而不是在同一台服务器上写入。

标签: node.jssails.js

解决方案


您可以使用依赖项sails-hook-uploads,因为您可以dirpath在设置中定义:

https://github.com/sailshq/sails-hook-uploads/blob/master/index.js#L31

module.exports.upload = {
  dirpath: '.tmp/public', // recommended .tmp/uploads
  adapter: require('skipper-disk'), // Default
}

我强烈建议不要使用公用文件夹,因为它是已编译的资产目的地,因此您可能会丢失这些文件。

作为替代方案,您可以使用控制器上传文件,如下所示:

https://github.com/mikermcneil/ration/blob/master/api/controllers/things/upload-thing.js#L54

之后,将其发送给用户:

https://github.com/mikermcneil/ration/blob/master/api/controllers/things/download-photo.js#L50


推荐阅读