在我的Angular项目中,我在上传之前使用了ngx-image-cropper来裁剪图像.component.ts的一些相关部分包括:

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 action:

public async Task<IActionResult> PostImage([FromForm] IFormFile file, [FromRoute] int id){
    ...
}

问题是,当我调试API时,input file参数为null,而cropped image的字符串值为base64.我该怎么修?

推荐答案

this.http.post需要在this.Base64ToFile(..)个电话中的.then(...)

你不会等到async fetch在API post之前完成

Javascript相关问答推荐

如何访问沙箱iframe内嵌套iframe的iframe. contentWindows. doctor?

单击另一个按钮后启用按钮

为什么我的useDispatch挂钩在这里设置不正确?

使用reaction创建可排序的表不起作用

foreach循环中的Typescript字符串索引

使用TMS Web Core中的HTML模板中的参数调用过程

过滤对象数组并动态将属性放入新数组

如何在Angular中插入动态组件

我可以从React中的出口从主布局调用一个处理程序函数吗?

在执行异步导入之前判断模块是否已导入()

如何强制Sphinx中的自定义js/css文件始终加载更改而不缓存?

IF语句的计算结果与实际情况相反

如何在Angular拖放组件中同步数组?

如何将zoom 变换应用到我的d3力有向图?

Reaction Redux&Quot;在派单错误中检测到状态Mutations

以编程方式聚焦的链接将被聚焦,但样式不适用

如何确保预订系统跨不同时区的日期时间处理一致?

当代码另有说明时,随机放置的圆圈有时会从画布上消失

在渲染turbo流之后滚动到元素

用另一个带有类名的div包装元素