UNPKG

@nbxx/nb-input

Version:
56 lines 2.02 kB
import { HttpClient, HttpEventType, HttpRequest, HttpResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { SystemAttachment } from './nbinput.entity'; import { filter, map } from 'rxjs/operators'; export class NbinputUploadService { constructor(http) { this.http = http; } upload(item, url) { const formData = new FormData(); formData.append('file', item); const req = new HttpRequest('POST', url, formData, { reportProgress: true, }); return this.http.request(req).pipe(filter(e => e instanceof HttpResponse || e.type === HttpEventType.UploadProgress), map(e => { const a = new SystemAttachment(); if (e.type === HttpEventType.UploadProgress) { a.completed = false; a.success = false; a.progress = Math.round(100 * e.loaded / e.total); a.url = ''; a.name = ''; a.size = 0; a.type = ''; a.id = 0; a.status = '上传中... ...'; } else if (e instanceof HttpResponse) { a.completed = true; a.success = true; a.progress = 100; a.url = e.body['data']['url']; a.type = e.body['data']['type']; a.name = e.body['data']['name']; a.size = e.body['data']['size']; a.id = e.body['data']['id']; a.status = '上传成功'; } return a; })); } blobToFile(theBlob, fileName) { const b = theBlob; b.lastModifiedDate = new Date(); b.name = fileName; return b; } } NbinputUploadService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ NbinputUploadService.ctorParameters = () => [ { type: HttpClient } ]; //# sourceMappingURL=nbinput-upload.js.map