如何在angular2中执行批量上传

时间:2016-11-02 12:14:37

标签: angular typescript

我正在尝试在angular2中使用批量上传,我用Google搜索但仍然不清楚。有人请发帖给我帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

尝试此代码实现

<input type="file" (change)="fileChange($event)" placeholder="Upload files" multiple>

fileChange(event) {
    let fileList: FileList = event.target.files;
    let fileListLength = fileList.length;
    if(fileListLength > 0) {
        let formData:FormData = new FormData();
        for (var i = 0; i < fileListLength; i++) {
            formData.append("uploadFile[]", fileList[i]);
        }
        let headers = new Headers();
        headers.append('Content-Type', 'multipart/form-data');
        headers.append('Accept', 'application/json');
        let options = new RequestOptions({ headers: headers });
        this.http.post(`${this.apiEndPoint}`, formData, options)
            .map(res => res.json())
            .catch(error => Observable.throw(error))
            .subscribe(
                data => console.log('success'),
                error => console.log(error)
            )
    }
}