-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo14.js
More file actions
34 lines (28 loc) · 802 Bytes
/
Copy pathdemo14.js
File metadata and controls
34 lines (28 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* 文件上传
* curl --form upload=@/Users/admin/workspace/jstest/koaDemo/package.json http://127.0.0.1:3000
* pwd
*/
const Koa = require('koa');
const app = new Koa();
const koaBody = require('koa-body');
const os = require('os');
const path = require('path');
const main = async function(ctx) {
const tmpdir = os.tmpdir();
const filePaths = [];
const files = ctx.request.body.files || {};
for (let key in files) {
const file = files[key];
console.log(tmpdir)
const filePath = path.join(tmpdir, file.name);
const reader = fs.createReadStream(file.path);
const writer = fs.createWriteStream(filePath);
reader.pipe(writer);
filePaths.push(filePath);
}
ctx.body = filePaths;
};
app.use(koaBody({ multipart: true }));
app.use(main);
app.listen(3000);