我想将多个文件附加到API,对我来说只能发送一个文件。我捕获了所有文件和console.log(array(给我所有文件,但是我无法将所有文件发送到API,它只是发送一个文件。
我的HTML组件
<input type="file" name="howeworkfile[]" ngModel
#howeworkfile="ngModel" multiple="multiple"
(change)="getFile($event)"
>
我的组件TS
getFile(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
console.log(fileList);
let file: File = fileList[0];
let formData: FormData = new FormData();
formData.append('howeworkfile[]', file);
formData.append('class_id', 7);
formData.append('subject_id', 1);
formData.append('school_id', 77);
formData.append('name', 'sd');
formData.append('description', 'sgerhgdf');
formData.append('score', '2');
formData.append('weight', 200);
formData.append('type', 12);
formData.append('deadline', '2018-02-13');
this._http.post(this.url, formData)
.subscribe(respo => {
console.log(respo);
// console.log(data.file);
}, (err: HttpErrorResponse) => {
console.log(err);
});
}
}}
应该像
<input type="file" multiple="multiple" (change)="getFile($event)">
...
getFile(event) {
let fileList: FileList = event.target.files;
let formData: FormData = new FormData();
for (const file of fileList) {
formData.append('some name', file, file.name) // file.name is optional
}
...
}