import {Component,OnInit,Input,Output,EventEmitter} from '@angular/core'; import { filter } from 'rxjs/operators/filter'; // tslint:disable import { HttpRequest, HttpClient, HttpResponse } from '@angular/common/http'; import { CmMessageService } from './../../../../../../../src/components/cosmos.module'; import { UploadFile } from './../../../../../../components/base/upload/interface'; @Component({ selector: 'cm-upload-doc-8', templateUrl: './upload-doc-8.component.html', styleUrls: ['./upload-doc-8.component.css'] }) export class UploadDoc8Component implements OnInit{ ngOnInit() {} ngOnDestroy() {} uploading = false; fileList: UploadFile[] = []; constructor(private http: HttpClient, private msg: CmMessageService) {} beforeUpload = (file: UploadFile): boolean => { this.fileList.push(file); return false; } handleUpload() { const formData = new FormData(); this.fileList.forEach((file: any) => { formData.append('files[]', file); }); this.uploading = true; // You can use any AJAX library you like const req = new HttpRequest('POST', 'https://jsonplaceholder.typicode.com/posts/', formData, { // reportProgress: true }); this.http.request(req).pipe(filter(e => e instanceof HttpResponse)).subscribe((event: any) => { this.uploading = false; this.msg.success('upload successfully.'); }, (err) => { this.uploading = false; this.msg.error('upload failed.'); }); } }