Eggjs使用ctx.getFileStream进行文件上传。最近用eggjs上传文件,获取不到前端定义的字段,一直是空的。这里记录下前端代码handleRequest(param){letfileObj=param.file;//FormData对象letform=newFormData();//文件对象form.append('bizId',this.apartment.id);form.append('bizType','1');form.append('文件',fileObj);//注意这个地方要追在最后ctx}=这个;try{//★★★★★提示,前端的formData.append('filename',file)一定要放在最后,否则ctx.getFileStream()完成流为空时,stream.fields依然会保留让流;stream=awaitctx.getFileStream();constparams={bizId:stream.fields.bizId,bizType:stream.fields.bizType,url:'',submittedFileName:stream.filename,contextType:流。mimeType,ext:stream.filename.split('.')[1],};constuid=UUID.v4();const名称='tks/'+path.basename(`${uid}.${params.ext}`);constresult=awaitclient.put(name,stream);参数.url=结果.url;constres=awaitctx.model.TksFile.create(params);返回资源;}catch(err){返回错误;}}参考:https://blog.csdn.net/qq_3615...https://github.com/eggjs/egg/...
