node.js - 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 而不是在同一台服务器上写入。
解决方案
您可以使用依赖项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
推荐阅读
- javascript - 如何嵌套 Promise(Promise.all 内的 Promise.all)
- bash - Ubuntu 上的 ZFS 擦洗 - 系统性能黑客
- swift - Swiftlint 覆盖与 SPM 相关的项目设置
- c# - 如何将一个项目转换为另一个项目(“无法将类型'Project.Services ...'隐式转换为'Project.MVC ...')
- javascript - 如何在 reactjs / redux 的类组件之外调度?
- python - 我们如何使用 for 循环绘制函数的多行以最小化编码?
- json - 如何在返回的 JSON 中不显示外键 ID?
- angular - Angular 8 数字输入最小值/最大值
- php - 为什么我的柏林噪声会产生超出预期范围的值,并产生具有硬边的非重复瓷砖?
- python - 如何使用 Python 读取在线 docx 文件和 webscrape 表格内容