Egg.js using superagent to do file forwarding

Source: Internet
Author: User

' 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.