Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

egg form-data提交如果没有文件报错 cant found upload file

$
0
0

问题: 1.如果使用mutipart/formdata提交表单,表单如果没有文件的话,egg中ctx.getFileStream()会报错cant found upload file,那这样就无法获得stream对象,就无法获取其它的字段。怎么解决?(那个因为前端那边的需求是传文件和字段,文件是可以不提交的) 2.前端使用的是Angular4写的,用的FormData对象提交文件和字段,为什么egg中ctx.getFileStream()获得的stream中fields字段是空的。

请求的截图问题2中前端的代码

sendplayRequest() {
  let formData: FormData = new FormData();
  if(this.playRequest.file){
  	formData.append("file",this.playRequest.file)
  }
  formData.set("MsgTitle",this.playRequest.MsgTitle)
  this.apiService.PlayRequest(formData).subscribe(res => {
  console.log(res);
  })
}

PlayRequest(formData: FormData){
  return this.http
  .post(this.host + "/play",formData)
  .map(res => res.json());
}

发出的请求头

服务端代码:

const config = ctx.app.config;
//获取上传的表单文件流以及表单字段
let stream;
try{
  stream = await ctx.getFileStream();
}catch(e) {
  console.log(e);
  return await next()
}
console.log(stream.fields;)

综上,希望有大神可以帮我解惑


Viewing all articles
Browse latest Browse all 14821

Trending Articles