|
import Multipart from '@multipart/form-data'
import Goa from '@goa/koa'
const app = new Goa()
const multipart = new Multipart({
dest: 'temp',
})
const middleware = multipart.single('file')
app.use(middleware)
app.use((ctx) => {
console.log('Fields: %O', ctx.req.body)
delete ctx.req.file.stream
console.log('File: %O', ctx.req.file)
})
|
Fields: { hello: 'world', name: 'multipart' }
File: { fieldname: 'file',
originalname: 'test.txt',
encoding: '7bit',
mimetype: 'application/octet-stream',
destination: 'temp',
filename: 'afb49cada5f721d7fa8337f072d03ec5',
path: 'temp/afb49cada5f721d7fa8337f072d03ec5',
size: 12 }
|
|
import Multipart from '@multipart/form-data'
import Goa from '@goa/koa'
const app = new Goa()
const multipart = new Multipart({
dest: 'temp',
preservePath: true,
})
const middleware = multipart.array('file', 2)
app.use(middleware)
app.use((ctx) => {
log('Fields', ctx.req.body)
log('Files', ctx.req.files)
})
|
Fields: { hello: 'world', name: 'multipart' }
Files: [ { fieldname: 'file',
originalname: 'test/fixture/test.txt',
encoding: '7bit',
mimetype: 'application/octet-stream',
destination: 'temp',
filename: '0fa202db40',
path: 'temp/0fa202db40',
size: 12 },
{ fieldname: 'file',
originalname: 'test/fixture/test.txt',
encoding: '7bit',
mimetype: 'application/octet-stream',
destination: 'temp',
filename: '149e4b08d6',
path: 'temp/149e4b08d6',
size: 12 } ]
|
fields(Array<FormDataField>): Accept files according to the configured fields and place them in a hashmap.
Click to show the FormDataField interface.
FormDataField: The item to use in the .fields method.
| name* | string | The name of the field. | | maxCount | number | The maximum count of the field. |
|
import Multipart from '@multipart/form-data'
import Goa from '@goa/koa'
const app = new Goa()
const multipart = new Multipart({
dest: 'temp',
})
const middleware = multipart.fields([
{ name: 'file', maxCount: 2 },
{ name: 'picture', maxCount: 1 },
])
app.use(middleware)
app.use((ctx) => {
log('Fields', ctx.req.body)
log('Files', ctx.req.files)
})
|
Fields: { hello: 'world', name: 'multipart' }
Files: { file:
[ { fieldname: 'file',
originalname: 'test.txt',
encoding: '7bit',
mimetype: 'application/octet-stream',
destination: 'temp',
filename: '13093f0764',
path: 'temp/13093f0764',
size: 12 },
{ fieldname: 'file',
originalname: 'test.txt',
encoding: '7bit',
mimetype: 'application/octet-stream',
destination: 'temp',
filename: '22e2e6e6f7',
path: 'temp/22e2e6e6f7',
size: 12 } ],
picture:
[ { fieldname: 'picture',
originalname: 'large.jpg',
encoding: '7bit',
mimetype: 'application/octet-stream',
destination: 'temp',
filename: '352a1aea6a',
path: 'temp/352a1aea6a',
size: 1592548 } ] }
|
none(): Do not accept files, only fields.
|
import Multipart from '@multipart/form-data'
import Goa from '@goa/koa'
const app = new Goa()
const multipart = new Multipart({
dest: 'temp',
})
const middleware = multipart.none()
app.use(middleware)
app.use((ctx) => {
log('Fields', ctx.req.body)
log('Files', ctx.req.files)
})
|
Fields: { hello: 'world', name: 'multipart' }
Files: undefined
|
any(): Accept all files and fields.
|
import Multipart from '@multipart/form-data'
import Goa from '@goa/koa'
const app = new Goa()
const multipart = new Multipart({
dest: 'temp',
})
const middleware = multipart.any()
app.use(middleware)
app.use((ctx) => {
log('Fields', ctx.req.body)
log('Files', ctx.req.files)
})
|
Fields: { hello: 'world', name: 'multipart' }
Files: [ { fieldname: 'file',
originalname: 'test.txt',
encoding: '7bit',
mimetype: 'application/octet-stream',
destination: 'temp',
filename: '7218bd891a',
path: 'temp/7218bd891a',
size: 12 },
{ fieldname: 'picture',
originalname: 'large.jpg',
encoding: '7bit',
mimetype: 'application/octet-stream',
destination: 'temp',
filename: 'e7a8050980',
path: 'temp/e7a8050980',
size: 1592548 } ]
|