【发布时间】:2019-07-27 06:07:56
【问题描述】:
当查看其他答案和一些谷歌时,一切似乎都很好,但我的控制器从未收到任何数据。
Api uris 等正确,请求到达正确的控制器
角度 sn-p:
component.html - 我的输入字段
<div class="input-group">
<input type="file" #fileInput id="fileInput" (change)="stageFile()"
accept="csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel">
<div class="input-group-append">
<button class="btn btn-primary" type="button" (click)="fileUpload()" [disabled]="!staged || uploading">
<span *ngIf="!uploading,else loadAnim">Upload</span>
<ng-template #loadAnim>Uploading...</ng-template>
</button>
</div>
</div>
component.ts - 从视图中获取数据
@ViewChild('fileInput') fileInput;
private file: File;
public uploading = false;
public staged = false;
constructor(private uploadService: UploadService) { }
public stageFile(): void {
this.staged = true;
this.file = this.fileInput.nativeElement.files[0];
console.log(this.file)
}
public fileUpload():void {
this.uploading = true;
if (this.file != null)
this.uploadService.upload(this.file).subscribe();
this.staged = false;
this.uploading = false;
}
services.ts - 处理实际的 ajax 调用
private uploadURI = environment.dataServiceURI + '/upload';
constructor(private http: HttpClient) {}
public upload(file: File): Observable<object> {
// create multipart form for file
let formData: FormData = new FormData();
formData.append('file', file, file.name);
const headers = new HttpHeaders().append('Content-Type', 'mulipart/form-data');
// POST
return this.http
.post(this.uploadURI, formData, {headers: headers})
.pipe(map(response => response));
}
.net core sn-p
这里 IFormFile 文件总是包含 null,因此我的结果总是 500
[HttpPost("upload")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult ParseData([FromForm(Name = "file")] IFormFile file)
{
if (file == null)
return StatusCode(500);
(...)
return Ok()
}
请求负载信息
来自浏览器网络
------WebKitFormBoundarycBigaNKzS4qNcTBg
Content-Disposition: form-data; name="file"; filename="test_data_schema.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
------WebKitFormBoundarycBigaNKzS4qNcTBg--
【问题讨论】:
-
当您查看浏览器的网络选项卡时,它会发送您的文件吗?只是为了检查问题是来自您的前端还是后端......
-
是的,它似乎发送了文件,至少请求有效负载包含一个 Content-Disposition 和一个 Content-Type 以及正确的表单数据值。即使有效载荷是空的,我也不知道为什么。有什么方法可以检查它是否真的发送文件而不仅仅是文件名?在发送之前将文件记录到控制台会给我一个以字节为单位的大小,但这就是我得到的全部。
-
您的代码中有一个错字: const headers = new HttpHeaders().append('Content-Type', 'mulipart/form-data'); --> “多部分”
-
我检查了我的 dotnet 代码: public Task
Backup(IFormFile file) 也许你应该删除 FromForm 选项,因为它是基于约定的(有效负载中的'file'将匹配参数中的'file' ) -
为我的错误提供资金(感谢@hugo)我仔细查看了我的标题,不仅有错字,还有错误。它必须是 Content-Disposition 而不是 Content-Type
标签: angular asp.net-core asp.net-core-webapi