【发布时间】:2019-02-20 23:37:36
【问题描述】:
我正在尝试将 CSV 文件传递到我的后端,以利用库来解析 CSV 数据。
我有一个 Angular7 前端和一个 C# .Net Core MVC 后端,它利用打字稿中的 HttpClient 来往后端传递数据和对象。
我尝试了许多解决方案,但收效甚微(最接近的是我可以在代码中访问控制器,但文件永远不会被传入)。
我试图避免对 CSV 进行任何类型的序列化或转换,直到它到达控制器,所以我想找到一个可以将整个 CSV 发送到后端的解决方案。
任何帮助将不胜感激。
service.ts
// Parse CSV
// The CSV always reaches this part of the code and I can inspect it.
parseCsv$(file: File): Observable<Dto> {
const request$ = this.http.post<Dto>(
location.origin + '/api/csvimport',
file,
{ headers: new HttpHeaders({ 'Content-Type': 'multipart/form-data' }) }
).pipe(
map((response: <Dto>) => response)
);
request$.subscribe();
return request$;
}
CsvImportController.cs
[HttpPost]
[ProducesResponseType(typeof(Dto), 200)]
// The 'file' variable below is always null
public async Task<IActionResult> ParseCsv([FromBody]IFormFile file)
{
// do work here
【问题讨论】:
标签: c# csv asp.net-core file-upload angular7