' use strict '; ConstController= require(' Egg ').Controller; ConstRequest= require(' superagent '); classApicontrollerextendsController{Asyncfile(){ Const {CTx,App} = This; //egg built-in interface gets the uploaded file, reference address: ConstStream=AwaitCTX.GetFileStream(); ConstUrl= ' Http://demo/api/file '; //Attach need to pass 3 parameters, the official document said that the file name is not required, but this must be transmitted, do not pass the words on the receiving end may not be recognized resulting in file upload failureAwait request.Post(URL).Set(' Content-type ', ' Multipart/form-data; ') .Attach(' file ', Stream.Read(), Stream.filename) . Then(Res= { Console.Log(' upload success ',Res; CTX.Body = Res.text; }).Catch(E= { Console.Log(' upload failed ',E; CTX.Body = { msg: ' upload failed ' }; }); } }
Address Resource Links
Superagent Official documents
http://visionmedia.github.io/superagent/
Egg File Upload Reference document
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
Egg.js using superagent for file forwarding