请求处理

文件上传

1
const awaitWriteStream = require('await-stream-ready').write;
2
const sendToWormhole = require('stream-wormhole');
3
...
4
const stream = await ctx.getFileStream();
5
6
// 文件生成绝对路径
7
const filename =
8
md5(stream.filename) + path.extname(stream.filename).toLocaleLowerCase();
9
10
const target = path.join(this.config.baseDir, 'app/public/uploads', filename);
11
12
// 生成一个文件写入文件流
13
const writeStream = fs.createWriteStream(target);
14
try {
15
// 异步把文件流写入
16
await awaitWriteStream(stream.pipe(writeStream));
17
} catch (err) {
18
// 如果出现错误,关闭管道
19
await sendToWormhole(stream);
20
throw err;
21
}
22
...
Copied!
Last modified 2yr ago
Copy link
Contents
文件上传