How does koa-body get the information uploaded to the front-end page file?

Let"s briefly describe the project first

  • spa application of version 6.x of view layer angualr.js
  • angular calls the api API of node directly, and node invokes the service to add, delete, modify and query
  • node version 8.11.x mainly uses koa2 framework and koa-body (^ 4.0.4), koa-router (^ 7.1.1) as request resolution

requirements are as follows

  • now there is a file upload requirement, but node is only used for forwarding and does not actually save the file, but calls the service api to operate
// angular 
let formData:FormData = new FormData();
    formData.append("file",this.fileData);  // this.fileinput file 
    formData.append("name","");
    this.http.upload("assets/upload/add",formData, (data) => {
      console.log(data)
    }, (error) => {

    });
// node
const koaBody = require("koa-body");    
app.use(koaBody(
    multipart: true,
    formidable: {
      maxFileSize: 200 * 1024 * 1024 // 2M
    }
));

router.post("/upload/add", async (ctx, next) => {
  console.log(ctx.request.files);  // undefined
  console.log(ctx.request.body);   // {}
  ctx.body = JSON.stringify(ctx.request.files); // 
});

foreground page setup request header "Content-Type":" multipart/form-data;" sends data normally, and everything is normal.
now I want to upload the file to the server by calling the server api if I don"t store any files on the node side, but ctx.request.files doesn"t have any data. If there are other ways, you can also share

.

if it is version 4 of koabody
const file = ctx.request.files.file; / / get uploaded file

if it is version 3 of koabody
const file = ctx.request.file; / / get uploaded file

Menu