我有模块以 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/