根据egg官方文档,https://eggjs.org/zh-cn/basics/controller.html#%E8%8E%B7%E5%8F%96%E4%B8%8A%E4%BC%A0%E7%9A%84%E6%96%87%E4%BB%B6
使用流模式上传文件,直接复制官方代码,postman跑文件上传接口出错:
代码如下:
async upload() {
const ctx = this.ctx;
const stream = await ctx.getFileStream(); // 此处未通过
const name = 'egg-multipart-test/' + path.basename(stream.filename);
// 文件处理,上传到云存储等等
let result;
try {
result = await ctx.oss.put(name, stream);
} catch (err) {
// 必须将上传的文件流消费掉,要不然浏览器响应会卡死
await sendToWormhole(stream);
throw err;
}
ctx.body = {
url: result.url,
// 所有表单字段都能通过 `stream.fields` 获取到
fields: stream.fields,
};
}
返回结果:
the multipart request can't be consumed twice
Hello @Erchoc. Please provide a reproducible example by creating a github repo.
Issues labeled by Need Reproduce will be closed if no activities in 7 days.
If you using the stream mode, please don't set mode to file
// config/config.default.js
exports.multipart = {
mode: 'stream',
};
@egg-bot @fengmk2 yeah i've got the same problem ,but this problem was habbened on prod env, when i was debugging in local env,the problem was gone,that made me wonder。
got the same problem in prod env too
Please follow the document.
If you using the
streammode, please don't set mode tofile// config/config.default.js exports.multipart = { mode: 'stream', };Thank you ,sir
Most helpful comment
If you using the
streammode, please don't set mode tofile