在Angular 8中,如果要将FormData提交到ASP.NET Core API进行IEnumerable/List绑定,可以按照以下步骤进行操作:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ApiService {
apiUrl = 'your-api-url';
constructor(private http: HttpClient) { }
uploadData(formData: FormData): Observable {
return this.http.post(`${this.apiUrl}/api/upload`, formData);
}
}
import { Component } from '@angular/core';
import { ApiService } from './api.service';
@Component({
selector: 'app-upload',
templateUrl: './upload.component.html',
styleUrls: ['./upload.component.css']
})
export class UploadComponent {
file: File;
constructor(private apiService: ApiService) { }
onFileChange(event) {
this.file = event.target.files[0];
}
upload() {
const formData = new FormData();
formData.append('file', this.file);
this.apiService.uploadData(formData).subscribe(
response => {
console.log(response);
},
error => {
console.log(error);
}
);
}
}
[Route("api/[controller]")]
[ApiController]
public class UploadController : ControllerBase
{
[HttpPost]
public async Task Upload(IFormFile file)
{
// 处理上传的文件,例如保存到服务器或者数据库中
return Ok();
}
}
在上述代码中,Upload
方法接收一个IFormFile
参数,用于接收上传的文件。你可以根据自己的需求对上传的文件进行进一步处理。
通过以上步骤,你就可以将FormData提交到ASP.NET Core API进行IEnumerable/List绑定了。