我正在尝试将包含输入文件和另一个属性的viewmodel绑定到mvc路由,但是无论我尝试什么都导致失败(没有错误,只有在调用路由时才为空值)。
public class TdaFileUploadRequest
{
public long TdaId { get; set; }
public IFormFile File { get; set; }
}
<form #fileForm="ngForm" (ngSubmit)="addFile()" enctype="multipart/form-data">
<div class="row">
<div class="col-md-10">
<div class="form-group select-group">
<label class="control-label col-form-label">TDA</label>
<select name="profile" [(ngModel)]="selectedTda" (ngModelChange)="selectTdaItem(selectedTda)">
<option *ngFor="let tda of tdaSelectItems" [ngValue]="tda.id">
{{ tda.name }}
</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="row margin-tspace-10">
<div class="col-md-8">
<input #fileInput class="form-control" type="file" accept=".xls,.xlsx"/>
</div>
</div>
<div class="row margin-tspace-10">
<button class="btn btn-primary" type="submit">Upload File</button>
</div>
</div>
</form>
仅显示相关代码。 {I} result
的{{1}}在步进时间问题时是不确定的......时间问题?我正在订阅服务中的观察者......我想。
.subscribe(result =>
我也试过设置标题选项,但这没有帮助。这些值也符合预期。
..设置标题(但后来删除了以下内容:
addFile(form: NgForm): void {
try {
const fi = this.fileInput.nativeElement;
if (fi.files && fi.files[0]) {
const fileToUpload = fi.files[0];
let status = "Not Set";
let message = "File uploaded successfully!";
let tdaFileUploadRequest = {
tdaId: this.selectedTda,
file: fileToUpload
};
this.tdaFileService
.uploadFile(tdaFileUploadRequest)
.subscribe(result => {
if (result) {
status = result['status'] || 'status not available';
message = result['message'] || 'no messages';
}
if (status === errorStatus) {
this.showError(message);
}
else
{
this.showSuccess(message);
}
}, error => this.handleError(error));
}
} catch (e) {
console.log(e || 'file-upload.component error');
}
}
let headers: Headers = new Headers({ 'Content-Type': 'multipart/form-data' });
最后是mvc路线。路由按预期调用,但 uploadFile(formData: any): Observable<any> {
let uploadResult: any;
const input = new FormData();
input.append("tdaId", formData.tdaId);
input.append("file", formData.file);
return this.httpClient.post<IUploadResult>(this.baseUrl + 'api/TdaFile/UploadTdaFile', formData);
}
为空
tdaFileUploadRequest