midwayjs / midway

🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
https://www.midwayjs.org/
MIT License
7.35k stars 575 forks source link

NotFoundError: uploadController is not valid in current context #1155

Open liuyanzhi1214 opened 3 years ago

liuyanzhi1214 commented 3 years ago

使用midway/faas复现egg的文件上传功能 const fs = require('mz/fs'); import { basename } from 'path'; import { Context } from 'egg'; import { Controller, Inject, Post } from '@midwayjs/decorator'; @Controller('/') export class uploadController {

@Inject() ctx: Context;

@Post('/upload') async uplaodFile() { const file = this.ctx.request.files[0]; const name = 'upload/' + basename(file.filename); let result; try { // 处理文件,比如上传到云端 result = await this.ctx.oss.put(name, file.filepath); } finally { // 需要删除临时文件 await fs.unlink(file.filepath); }

return {
  url: result.url,
  // 获取所有的字段值
  requestBody: this.ctx.request.body,
};

} }

出现报错: NotFoundError: uploadController is not valid in current context at MidwayContainer.getAsync (E:\Midway\faas_downupload\node_modules\@midwayjs\core\dist\context\applicationContext.js:175:19) at MidwayContainer.getAsync (E:\Midway\faas_downupload\node_modules\@midwayjs\core\dist\context\midwayContainer.js:475:33) at MidwayRequestContainer.getAsync (E:\Midway\faas_downupload\node_modules\@midwayjs\core\dist\context\requestContainer.js:78:32) at MidwayFaaSFramework.invokeHandler (E:\Midway\faas_downupload\node_modules\@midwayjs\faas\dist\framework.js:169:56) at E:\Midway\faas_downupload\node_modules\@midwayjs\faas\dist\framework.js:117:51 at dispatch (E:\Midway\faas_downupload\node_modules\koa-compose\index.js:42:32) at E:\Midway\faas_downupload\node_modules\koa-static-cache\index.js:39:69 at dispatch (E:\Midway\faas_downupload\node_modules\koa-compose\index.js:42:32) at E:\Midway\faas_downupload\node_modules\koa-compose\index.js:34:12 at E:\Midway\faas_downupload\node_modules\@midwayjs\faas\dist\framework.js:123:39 {

}

如何解决呢?

czy88840616 commented 3 years ago

uploadController 缺少 @Provide() 装饰器。

ZQun commented 3 years ago

@czy88840616 faas支持上传了?

czy88840616 commented 3 years ago

有限制,少量上传或者oss前端直传

liuyanzhi1214 commented 3 years ago

uploadController 缺少 @provide() 装饰器。

按照您说的修改了,可是又有新的问题Orz TypeError: Cannot read property '0' of undefined at uploadController.uplaodFile (E:\Midway\Midway\faas\src\function\upload.ts:16:40) at MidwayFaaSFramework.invokeHandler (E:\Midway\Midway\faas\node_modules\@midwayjs\faas\dist\framework.js:174:56) at processTicksAndRejections (internal/process/task_queues.js:95:5) at FCRuntime.invokeHandlerWrapper (E:\Midway\Midway\faas\node_modules\@midwayjs\runtime-engine\dist\lightRuntime.js:19:28) at invoke (E:\Midway\Midway\faas\node_modules\@midwayjs\serverless-app\dist\framework.js:240:36)

liuyanzhi1214 commented 3 years ago

或者faas是否支持使用 formidable 来实现文件上传?

czy88840616 commented 3 years ago

函数无法支持 formidable 上传,函数网关 + 弹性本身限制,无法越过。

liuyanzhi1214 commented 3 years ago

采用multer中间件的方式呢?

czy88840616 commented 3 years ago

这个跟中间件没关系吧,核心原因是网关会把2m以内上传的文件,做base64编码,没有流式机制。

liuyanzhi1214 commented 3 years ago

koa-hooks-react是否也支持使用koa的接口来实现文件上传呢?

Lxxyx commented 3 years ago

koa-hooks-react是否也支持使用koa的接口来实现文件上传呢?

理论上是可行的,还没有试过。后续我会将这些周边的需求和方案梳理一遍

liuyanzhi1214 commented 3 years ago

koa-hooks-react中为什么无法解析url中的参数 例如:http://127.0.0.1:3000/api/download?name=testFile.rar 似乎将download?name=testFile.rar当作路由来识别 export default async () => { const ctx = useContext(); let arg = url.parse(ctx.url).query; let params = querystring.parse(arg); let name = params.name; console.log(name) const path = download/${name}; //const path = download/testFile.rar; ctx.attachment(path); await send(ctx, path); };