koa文件上传中间件——koa-multer

koa-multer用法基本和multer一致,npm里koa-multer的用法介绍比较简单,可以参考multer的用法

 

使用

const Koa = require('koa');
const Router = require('koa-router');
const multer = require('koa-multer');
const path = require('path');

const server = new Koa();

let storage = multer.diskStorage({
    destination: path.resolve('upload'),
    filename: (ctx, file, cb)=>{
        cb(null, file.originalname);
    }
});
let fileFilter = (ctx, file ,cb)=>{
//过滤上传的后缀为txt的文件
    if (file.originalname.split('.').splice(-1) == 'txt'){
        cb(null, false); 
    }else {
        cb(null, true); 
    }
}
let upload = multer({ storage: storage, fileFilter: fileFilter });

let router = new Router();
router.post('/upload', upload.single('file'), async ctx => {
    if (ctx.req.file){
        ctx.body = 'upload success';
    } else {
        ctx.body = 'upload error';
    }
});
server.use(router.routes());

server.listen(8080, ()=>{
    console.log('usage: curl http://localhost:8080/upload -F "file=@1.jpg"');
});

测试:

curl http://localhost:8080/upload -F "file=@1.jpg"
>> upload success
curl http://localhost:8080/upload -F "file=@1.txt"
>> upload error

 

说明
文件信息
KeyDescription Note
fieldnameField name specified in the form
originalnameName of the file on the user’s computer
encodingEncoding type of the file
mimetypeMime type of the file
sizeSize of the file in bytes
destinationThe folder to which the file has been saved DiskStorage
filenameThe name of the file within the destination DiskStorage
pathThe full path to the uploaded file DiskStorage
bufferA Buffer of the entire file MemoryStorage
multer(opts)

opts参数

KeyDescription
dest or storageWhere to store the files
fileFilterFunction to control which files are accepted
limitsLimits of the uploaded data
preservePathKeep the full path of files instead of just the base name
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
koa-body中间件是一个解析HTTP请求body的中间件,用于获取POST请求或PUT请求提交的数据。它可以解析多种格式的数据,如JSON、表单以及多部分数据等。 koa-body中间件使用了co-body来解析HTTP请求body。co-body是一个用于对HTTP请求body进行解析的库,它支持多种格式的数据解析,包括JSON、URL-encoded表单、多部分form-data等。 使用koa-body中间件非常简单,只需要在应用程序中引入并挂载它即可: ``` const Koa = require('koa'); const koaBody = require('koa-body'); const app = new Koa(); app.use(koaBody()); ``` 这样,koa-body中间件就被挂载到了应用程序中,并且在每个HTTP请求到达时自动处理请求的body数据。 koa-body中间件的常用配置选项包括: - `patchNode`:指定在解析JSON对象时是否使用Object.assign()函数进行patch操作,缺省为true。 - `jsonLimit`:指定解析JSON数据的最大字节数,默认为1MB。 - `formLimit`:指定解析表单数据的最大字节数,默认为56KB。 - `textLimit`:指定解析纯文本数据的最大字节数,默认为1MB。 - `encoding`:指定请求体编码,默认为utf-8。 - `multipart`:指定是否解析multipart请求体,默认为false。 - `urlencoded`:指定是否解析URL-encoded表单请求体,默认为true。 - `formidable`:指定是否启用formidable解析multipart请求体,默认为true。 举例: ``` app.use(koaBody({ jsonLimit: '1mb', multipart: true, formidable:{ keepExtensions: true, // 保留扩展名 maxFieldsSize: 2 * 1024 * 1024, // 文件上传大小 } })) ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值