c# - 如何从 Angular Reactive Form 接收文件上传?

标签 c# asp.net .net-core angular7 angular-reactive-forms

我有模块以 Angular 7 react 形式上传文件(我需要 react 形式,因为我需要一起上传文件和一些其他信息)

我关注这篇文章:https://medium.com/@amcdnl/file-uploads-with-angular-reactive-forms-960fd0b34cb5

代码如下:https://pastebin.com/qsbPiJEX

onFileChange(event) {
    const reader = new FileReader();

    if(event.target.files && event.target.files.length) {
      const [file] = event.target.files;
      reader.readAsDataURL(file);

      reader.onload = () => {
        this.formGroup.patchValue({
          file: reader.result
       });

        // need to run CD since file load runs outside of zone
        this.cd.markForCheck();
      };
    }
}

据我所知,我将收到文件作为 json 数据中的文本。 但我不知道如何将其作为文件接收或将 json 数据转换为文件。文件可以是图片、pdf或其他文档(excel、docs或其他格式)

我正在使用 Dotnet Core 2.2 和 Angular 7 知道如何接收文件吗?

最佳答案

我有一个表单,我想在其中通过 formData 发布图像,我只是通过放置此属性 writeFile="true" 在我的 FormControl 中获得了文件对象。这可以将 FileList 对象写入您的 FormControl 中作为值。为此,您需要安装“@rxweb/reactive-form-validators”包并注册“RxReactiveFormsModule”模块。就是这样。

这是我的 html 代码:

<form  [formGroup]="userFormGroup">       

        <label>Profile Photo</label>
        <input type="file" [writeFile]="true" formControlName="profilePhoto" multiple />        

      <button [disabled]="!userFormGroup.valid" class="btn btn-primary">Submit</button>
    </form>

请引用这个例子:Stackblitz

关于c# - 如何从 Angular Reactive Form 接收文件上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55291422/

相关文章:

按钮上的 C# 图标(调整大小)

c# - Windows 窗体 - 在窗体和控件之间传递参数

c# - 在 RavenDB 中指定集合名称

model-view-controller - 403 - 禁止 : Access is denied. ASP.NET Core MVC IIS 客户端证书 SSL

linux - .Net Core 2.2 jwt 验证在容器上失败并显示 401

java - 如何在 C# .NET Core 6 中使用 Java .dll?

c# - 在 c# (windows) 应用程序中不存在控件时显示 .gif 动画

asp.net - 使用 Google 货币转换 API 时输入字符串格式不正确

Asp.NET Web API - 405 - 不允许用于访问此页面的 HTTP 动词 - 如何设置处理程序映射

asp.net - DropDownList 的 SelectedIndexChanged 事件未触发