我想上传多个文件,并创建一个属性为multiple
的输入字段,但我无法选择多个文件。
app.html
<input type="file" (change)="onChange($event)" required class="form-control " multiple>
的 component.ts
onChange(event: any) {
let fileList: FileList = event.target.files;
console.log(event.target.files)
if(fileList.length > 0) {
let file: File = fileList[0];
let formData:FormData = new FormData();
formData.append('degree_attachment', file, file.name);
let headers = new Headers();
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
this.http.post('http://localhost:8080/EventManager/UploadFile',
formData,options)
.map(res => res.json())
.catch(error => Observable.throw(error))
.subscribe(
data => console.log('success'),
error => console.log(error)
)
}}
答案 0 :(得分:0)
应该能够选择多个文件。
现场演示:Plunker
代码:
@Component({
selector: 'my-app',
template: `
<form enctype='multipart/form-data'>
<input
(change)="onChange($event)"
type='file'
name='files[]'
multiple
/>
<button type='submit'>Submit</button>
</form>
`,
})
export class App {
onChange(event: any) {
let fileList: FileList = event.target.files;
console.log(event.target.files)
}}
}