上面的文字看起来可能不够直观,所以可以自己发送请求来查看:
//前台页面login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.css">
</head>
<body>
<form action="/fileupload" enctype="text/plain" method="POST">
<div class="form-group">
<label for="username" class="control-label">用户名</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password" class="control-label">密码</label>
<input type="text" class="form-control" id="password" name="password">
</div>
<div class="form-group">
<label for="avatar" class="control-label">头像</label>
<input type="file" multiple class="form-control" id="avatar" name="avatar">
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger">登录</button>
</div>
</form>
</body>
</html>
复制代码
//后台服务
const Koa = require('koa');
const path = require('path');
const Router = require('koa-router');
const static = require('koa-static');
const session = require('koa-session');
let app = new Koa();
let router = new Router();
app.keys = ['zfpx','jw'];
app.use(session({
maxAge:5*1000,
}, app));
app.use(static(path.resolve(__dirname)));
app.use(static(path.resolve(__dirname,'node_modules')));
router.post('/login',async(ctx,next)=>{
// 获取用户的账号及密码
ctx.session.user = ctx.request.body
ctx.body = ctx.session.user;
});
router.get('/home',async (ctx,next)=>{
if (ctx.session.user){
ctx.body = {
status:1,
username: ctx.session.user.username
}
}else{
ctx.body = {
status: 0,
username: null
}
}
})
router.post('/fileupload',async(ctx,next)=>{
console.log('upload')
})
app.use(router.routes());
app.listen(3000);
复制代码
<figure>[图片上传中...(image-d75618-1538878199379-6)]
<figcaption></figcaption>
</figure>
将login.html的form表单的enctype属性分别改成text/plain、application/x-www-form-urlencoded、multipart/form-data:
text/plain的请求体:空格转换为 "+" 加号,但不对特殊字符编码
<figure>[图片上传中...(image-41f4c-1538878199375-2)]
<figcaption></figcaption>
</figure>
application/x-www-form-urlencoded的请求体:在发送前编码所有字符(不设置默认)
<figure>[图片上传中...(image-45846c-1538878199375-1)]
<figcaption></figcaption>
</figure>
multipart/form-data:不对字符编码,在使用包含文件上传控件的表单时,必须使用该值。
<figure>[图片上传中...(image-49d391-1538878199375-0)]
<figcaption></figcaption>
</figure>
从上面的图片可以看出来:
//利用buffer来缓存数据,kao的中间件使用async和await
function bodyParser() {
return async (ctx, next) => {
await new Promise((resolve, reject) => {
let arr = [];
ctx.req.on('data', function (data) {
arr.push(data);
});
ctx.req.on('end', function () {
let r = Buffer.concat(arr).toString();
ctx.request.body = r;
resolve();
})
});
await next();
}
}
复制代码
<figure>[图片上传中...(image-85e875-1538878199378-5)]
<figcaption></figcaption>
</figure>
<figure>[图片上传中...(image-4741be-1538878199378-4)]
<figcaption></figcaption>
</figure>
从上面的两张图可以看到请求头中contentType中存在一个boundary属性,而请求体中的数据正是用这个boundary属性的值隔开的,去除里面的内容可以用分割的方法。由于文件是二进制的,需要分割buffer,但是Buffer的原型上没有这个方法,所以要扩展这个split方法:
Buffer.prototype.split = function (sep) {
let len = Buffer.from(sep).length; //分隔符的字节长度
let pos = 0;
let index = 0;
let arr = [];
//判断pos位后面是否还存在boundary
//截取boundary前面的内容放在数组中
while (-1 != (index = this.indexOf(sep,pos))) {
arr.push(this.slice(pos,index));
pos= len + index;//每个(**)b的位置
}
arr.push(this.slice(pos));//将最后boundary后面的内容放进数组
return arr;
}
//b代表boundary,**代表获取的内容
let buffer = Buffer.form('b**b**b**b--').split('b')
console.log(buffer);
//[<Buffer >,<Buffer 2a 2a>,<Buffer 2a 2a>,<Buffer 2a 2a>,<Buffer 2d 2d>]
复制代码
截取了需要的内容之后就可以做进一步的处理:
<figure>[图片上传中...(image-cea801-1538878199378-3)]
<figcaption></figcaption>
</figure>
function betterBody({uploadDir}) {
return async (ctx,next)=>{
await new Promise((resolve,reject)=>{
let arr = [];
ctx.req.on('data', function (data) {
arr.push(data);
});
ctx.req.on('end', function () {
if (ctx.get('content-type').includes('multipart')) {
let r = Buffer.concat(arr); //请求体中的内容
//获取的boundary少了--,加上后用于截取内容
let boundary = '--' + ctx.get('content-type').split('=')[1];
// 去除头尾取到中间有用的部分
//[<Buffer 2a 2a>,<Buffer 2a 2a>,<Buffer 2a 2a>]
let lines = r.split(boundary).slice(1, -1);
let fileds = {};
//处理含filename和不含filename的内容
lines.forEach((line) => {
let [head, content] = line.split('\r\n\r\n');
head = head.toString();
if (head.includes('filename')) {
// 是文件取出除head+'\r\n\r\n'后的内容
//-2表示去除最后空白行\r\n
let content = line.slice(head.length + 4, -2);
let uuid = require('uuid/v4');
let fs = require('fs');
let p = uuid();
fs.writeFileSync(path.resolve(__dirname, uploadDir, p), content);
fileds['path'] = p;
} else {
// 不是文件,挂载参数例如:username
let key = head.match(/name="([\s\S]*)"/im)[1];
//-2表示去除最后空白行\r\n
let value = content.toString().slice(0, -2);
fileds[key] = value;
};
})
ctx.request.fields = fileds;
resolve();
}
})
})
await next()
}
}
复制代码
koa框架会用也会写系列的内容基本就介绍完了,欢迎大家指正!
</article>
作者:梦想攻城狮
链接:https://juejin.im/post/5baf63a8f265da0acf0af160
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。