如何将文件从angular 10上载到asp.net core WebAPI 3.1
我正在尝试将Angular 10应用程序中的文件(图像或PDF)上载到dotNET Core 3.1 web API。 但是没有成功。下面是我的代码 WebAPI代码 当我点击上传。出现以下错误 有人能帮忙吗?实际上,此代码在dotNET Core 2.2中有效,但在3.1中无效。在.net Core 3中使用接口如何将文件从angular 10上载到asp.net core WebAPI 3.1,angular,asp.net-web-api,.net-core,file-upload,Angular,Asp.net Web Api,.net Core,File Upload,我正在尝试将Angular 10应用程序中的文件(图像或PDF)上载到dotNET Core 3.1 web API。 但是没有成功。下面是我的代码 WebAPI代码 当我点击上传。出现以下错误 有人能帮忙吗?实际上,此代码在dotNET Core 2.2中有效,但在3.1中无效。在.net Core 3中使用接口 [HttpPost] [Route("UploadDocument")] public async Task<IActionRe
[HttpPost]
[Route("UploadDocument")]
public async Task<IActionResult> UploadDocument([FromForm] IFormFile avatar)
{
using (var memoryStream = new MemoryStream())
{
await avatar.CopyToAsync(memoryStream);
// Upload the file if less than 2 MB
if (memoryStream.Length > 2097152)
{
ModelState.AddModelError("File", "The file is too large.");
return BadRequest(ModelState);
}
return Created("","");
}
}
[HttpPost]
[路线(“上传文件”)]
公共异步任务上载文档([FromForm]IFormFile avatar)
{
使用(var memoryStream=new memoryStream())
{
等待化身。复制同步(memoryStream);
//如果小于2 MB,请上载文件
如果(memoryStream.Length>2097152)
{
AddModelError(“文件”,“文件太大”);
返回请求(ModelState);
}
已创建的返回(“,”);
}
}
这可能会有所帮助。
<form [formGroup]="caseForm" (ngSubmit)="saveCase()">
<div class="row">
<div class="col-md-3">
Application Name
</div>
<div class="col-md-6">
<input type="text" class="form-control" formControlName="ApplicantName">
</div>
</div>
<div class="row mt-3">
<div class="col-md-3">
Select Document
</div>
<div class="col-md-6">
<input type="file" class="form-control" (change)="onFileSelected($event)">
<br>
<span [innerHTML]="result"></span>
</div>
</div>
<div class="row mt-3">
<div class="col-md-3">
</div>
<div class="col-md-3">
<button type="button" class="btn btn-block btn-secondary">Clear</button>
</div>
<div class="col-md-3">
<button type="submit" class="btn btn-block btn-primary">Save</button>
</div>
</div>
</form>
saveCase() {
if (this.caseForm.valid) {
const formData = new FormData();
formData.append("ApplicantName", this.caseForm.value.ApplicantName);
formData.append("file", this.selectedFile, this.selectedFile.name);
// Display the values
formData.forEach((value, key) => {
console.log(key + value)
});
this.http.post("https://localhost:44343/api/Case/UploadDocument", formData, {
reportProgress: true,
observe: 'events'
})
.subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
console.log("Upload Progress: " + Math.round(event.loaded / event.total * 100) + '%')
}
else if (event.type === HttpEventType.Response) {
console.log(event);
this.toastr.success('Case', 'Case Created Successfully');
}
this.resetCaseForm();
},
err => {
console.error(err);
});
}
}
[HttpPost]
[Route("UploadDocument")]
public async Task<IActionResult> UploadDocument([FromForm] IFormFile avatar)
{
using (var memoryStream = new MemoryStream())
{
await avatar.CopyToAsync(memoryStream);
// Upload the file if less than 2 MB
if (memoryStream.Length > 2097152)
{
ModelState.AddModelError("File", "The file is too large.");
return BadRequest(ModelState);
}
return Created("","");
}
}