koa-transfer-file
Package under development. Please lock
the specific version in package.json
or package-lock.json
.
Transfer file stream without storing files.
This package is mainly used for the middle layer.
Featured
-
option
onDisk
: (boolean, default true) It determines whether disk I/O is being used during transmission. ConvertingStream
toBuffer
by array is unsafe when transferring big files. To avoid this problem, using file stream as a default. The temp file will be deleted after newReadable
stream is built. -
maintain files' name: When sending files to another server, filenames will be changed into tmpName because of the new readable stream. Solved by adding property
name
to the readable stream, due to the packageform-data
will name the file byfilestream.name
orfilestream.path
when appending data. -
option
appendField
: (boolean, default false) Append files toctx.request.body
in order to keep it(formData) the same as before the request was sent. -
option
appendFile
: (boolean, default true, deprecative) Highly recommandedfalse
. Append all files in an array toctx.request.body
with fieldname_files
. The difference between ctx.request.body._files and ctx.request.files is that_files
has been formatted for the puropse of transferring directly by request.The default value is only for compatibility with the old versions temporarily. It's innocent when you don't care about files' fieldname.
Install
npm install koa-transfer-file
Usage
The options
almost same as busboy
.
const Koa = ;const transfer = ; const app = ; const options = onDisk: true // (boolean, default true) limits: fileSize: 1024 * 5 app;
Transfer
Transfer formData by request
directly.
const request = ; app;
Or configure the formData's value manually when opts.onDisk=false
.
const formData = {}; formDatafilefieldname = value: filevalue options: filename: filefilename contentType: filemimetype
Save
For each file
of ctx.request.files
:
- By default,
file
is a readable stream.
const rs = file;
- When
opts.onDisk
is set to false,file.value
contains a Buffer.
const Readable = ; const rs = ;rs {}; rs;
Then save the readable stream to the file.
rs ;;
API
Opts
Please refer to Featured for description of the options.
File information
Files (<object[]>) can be got from ctx.request.files
.
The properties of each file are as shown below.
Certain Properties
Key | Desc |
---|---|
filename | original name of the file |
fieldname | field name specified in the form |
encoding | - |
mimetype | - |
truncated | stream is truncated or not (file reached limit size) |
opts.onDisk = true
Key | Desc |
---|---|
name | alias of filename |
opts.onDisk = false
Key | Desc |
---|---|
value | file data in buffer |