32 lines
1012 B
TypeScript
32 lines
1012 B
TypeScript
import { Injectable } from '@angular/core';
|
|
import {HttpResponse} from "@angular/common/http";
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class FileService {
|
|
getFilenameFromHttpResponse(httpResponse: HttpResponse<Blob>) {
|
|
const contentDispositionHeader = httpResponse.headers.get('Content-Disposition');
|
|
// console.log(contentDispositionHeader);
|
|
let result = contentDispositionHeader.split(';')[1].trim().split('=')[1];
|
|
// Removing the " from the after trim operation
|
|
result = result.replace(/"/g, '');
|
|
// Removing . from filename
|
|
// return result.replace(/./g, '_');
|
|
|
|
return result;
|
|
}
|
|
|
|
downloadBlob(data: HttpResponse<Blob>) {
|
|
const url = window.URL.createObjectURL(data.body);
|
|
const anchor = document.createElement('a');
|
|
|
|
anchor.download = this.getFilenameFromHttpResponse(data);
|
|
anchor.href = url;
|
|
anchor.click();
|
|
anchor.remove();
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
}
|
|
}
|