@multipart/form-data
v1.0.1
Published
Multipart/Form-Data And File Upload Middleware For Koa Written In ES6 And Optimised With JavaScript Compiler.
Downloads
352
Maintainers
Readme
@multipart/form-data
@multipart/form-data
is Multipart/Form-Data And File Upload Middleware For Koa Written In ES6 And Optimised With JavaScript Compiler.
Originally, this was a Multer fork, however it was rewritten specifically for Koa2, and the interfaces were updated to be async rather than callbacks. Differences:
- When the file size limit is reached, the next middleware is called, rather than waiting to drain the request stream. This can result in the client-side EPIPE (connection reset) errors when sending files larger than allowed. But ideally, Node.JS applications should be run behind a proxy such as NginX to limit the upload size.
- Removes the unnecessary
typeis
dependency that includes themime-type
database, just checks the Content-Type to start withmultipart/form-data
. - Compiled with Google Closure Compiler and has just 1 dependency (
text-decoding
) to decode non-utf8 fields (e.g., when a form submitted had theaccept-charset
attribute).
yarn add @multipart/form-data
Table Of Contents
API
The package is available by importing its default and named functions:
import FormData, {
diskStorage, memoryStorage, FormDataError,
} from '@multipart/form-data'
class FormData
This class is used to create middleware according to the required file upload strategy.
FormData
: An instance to create middleware.
Creates a new instance according to the config. It is later used to access the middleware functions described below.
FormDataConfig
: The configuration for the instance.
| Name | Type | Description | Default |
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| dest | string | The directory where to store the files using the DiskStorage
. If not specified, files will be saved in the system's temp directory (os.tmpdir()
). | - |
| storage | FormDataStorageEngine | An instance of a custom storage engine. | - |
| fileFilter | FormDataFileFilter | The file filter. | - |
| limits | _goa.BusBoyLimits | The limits of the uploaded data. | - |
| preservePath | boolean | Whether to keep the full path of files instead of just the base name. | false
|
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 } ]
FormDataField
: The item to use in the .fields
method.
| Name | Type | Description | | --------- | --------------- | ------------------------------- | | 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 } ] }
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
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 } ]
FormDataFile
MultipartFormData adds a body
object and a file
or files
object to the request object. The body
hashmap contains the values of the text fields of the form, the file
or files
object contains the files uploaded via the form.
import('stream').Readable
stream.Readable
: A stream that pushes data when it becomes available.
FormDataFile
: The information about each file.
| Name | Type | Description |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------- |
| fieldname* | string | The field name specified in the form. |
| originalname* | string | The name of the file on the user's computer. |
| encoding* | string | The encoding type of the file. |
| mimetype* | string | The mime type of the file. |
| size* | number | The size of the file in bytes. |
| destination* | string | The folder to which the file has been saved. Set by DiskStorage. |
| filename* | string | The name of the file within the destination
. Set by DiskStorage. |
| path* | string | The full path to the uploaded file. Set by DiskStorage. |
| buffer* | Buffer | The Buffer
of the entire file. Set by MemoryStorage. |
| stream* | stream.Readable | The Readable stream with the file data. This stream should not be read other than by a storage engine. |
Copyright & License
GNU Affero General Public License v3.0
Original work by Multer's contributors under MIT license found in COPYING.