Facebook
From Sexy Rhinoceros, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 458
  1. import { Observable } from 'rxjs/Observable';
  2. import { Injectable } from '@angular/core';
  3. import {HttpClient, HttpRequest, HttpEvent} from '@angular/common/http';
  4.  
  5. @Injectable()
  6. export class UploadFileService {
  7.  
  8.   private filename: string;
  9.  
  10.   constructor(private http: HttpClient) { }
  11.  
  12.   pushFileToStorage(file: File): Observable<HttpEvent<{}>> {
  13.     const formData: FormData = new FormData();
  14.     formData.append('file', file);
  15.     this.filename = file.name;
  16.  
  17.     const req = new HttpRequest('POST', 'http://localhost:8080/post', formData, {
  18.       reportProgress: true,
  19.       responseType: 'text'
  20.     });
  21.  
  22.     return this.http.request(req);
  23.   }
  24.  
  25.   getFiles(): Observable<any> {
  26.     return this.http.get('http://localhost:8080/getAllFiles');
  27.   }
  28.  
  29.   setFilename(filename: string) {
  30.     this.filename = filename;
  31.   }
  32.  
  33.   getFilename(): string {
  34.     return this.filename;
  35.   }
  36.  
  37. }
  38.