Skip to content

Instantly share code, notes, and snippets.

@wesleygrimes
Created March 19, 2019 17:33
Show Gist options
  • Save wesleygrimes/ea319b4c52f8178d562feb2d6fec6d41 to your computer and use it in GitHub Desktop.
Save wesleygrimes/ea319b4c52f8178d562feb2d6fec6d41 to your computer and use it in GitHub Desktop.
ngrx-file-upload/src/app/file-upload.service.ts
import { HttpClient, HttpEvent, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class FileUploadService {
private API_BASE_URL = environment.apiBaseUrl;
constructor(private httpClient: HttpClient) {}
public uploadFile(file: File): Observable<HttpEvent<{}>> {
const formData = new FormData();
formData.append('files', file, file.name);
const options = {
reportProgress: true
};
const req = new HttpRequest(
'POST',
`${this.API_BASE_URL}/api/file`,
formData,
options
);
return this.httpClient.request(req);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment