首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >附加的FormData将空值发送给API

附加的FormData将空值发送给API
EN

Stack Overflow用户
提问于 2022-04-23 04:32:32
回答 1查看 157关注 0票数 -1

在上传之前,我已经在我的角度项目中使用了ngx-image-cropper来剪切图像。component.ts的一些相关部分如下:

代码语言:javascript
复制
croppedImage: any = '';
imageCropped(event: ImageCroppedEvent) {
  this.croppedImage = event.base64;
}
Base64ToFile(url: any, filename: any, mimeType: any){
    return (fetch(url)
            .then(function(res){return res.arrayBuffer();})
            .then(function(buf){return new File([buf], filename,{type:mimeType});})
        );
  }

 OnSubmit() {
    const filedata = new FormData();
    this.Base64ToFile(this.croppedImage, "myphoto.png", "image/png").then(function(outputFile){
      
      console.log(outputFile);
      filedata.append("file", outputFile, outputFile.name);//The first parameter "file" is the input parameter in API method
    });
    this.http.post('https://localhost:5001/api/featuredpost/postimage/' + this.selectedPostId, filedata).subscribe(
      response => {
        console.log(response);
      }
    );
  }

API操作:

代码语言:javascript
复制
public async Task<IActionResult> PostImage([FromForm] IFormFile file, [FromRoute] int id){
    ...
}

问题是,当我调试API时,输入的file参数为null,而cropped image有一个base64字符串值。我怎么才能修好它?我寻找相关的问题,但没有找到任何解决我的案件。A related question

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-23 05:27:13

this.http.post需要在this.Base64ToFile(..)调用的.then(...)

您没有等待async fetch在API post之前完成

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71976828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档