54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import { FormControl, FormGroup } from '@angular/forms';
|
|
|
|
export class CommonUtilities {
|
|
/** Display the blob */
|
|
static displayFileInNewTab(response:any, defaultFileName:string) {
|
|
const contentType = response.headers.get('content-type');
|
|
const blob = new Blob([response.body], { type: contentType });
|
|
const fileName = this.getFileName(response,defaultFileName);
|
|
const nav = (window.navigator as any);
|
|
if (nav.msSaveOrOpenBlob) {
|
|
nav.msSaveOrOpenBlob(blob, fileName);
|
|
} else {
|
|
const data = window.URL.createObjectURL(blob);
|
|
const link = document.createElement('a');
|
|
link.href = data;
|
|
link.target = '_blank';
|
|
link.click();
|
|
}
|
|
}
|
|
/*** Download the blob ***/
|
|
static downloadFile(response:any, defaultFileName:string) {
|
|
const contentType = response.headers.get('content-type');
|
|
const blob = new Blob([response.body], { type: contentType });
|
|
const fileName = this.getFileName(response,defaultFileName);
|
|
const nav = (window.navigator as any);
|
|
if (typeof nav.msSaveBlob === 'function')
|
|
{
|
|
console.log("download the report saveblob");
|
|
nav.msSaveBlob(blob, fileName);
|
|
console.log("download the report saveblob done");
|
|
}
|
|
else {
|
|
const link = document.createElement('a');
|
|
link.href = window.URL.createObjectURL(blob);
|
|
console.log("no msSaveBlob download the report saveblob done");
|
|
//link.download? = fileName;
|
|
link.download = fileName;
|
|
link.click();
|
|
}
|
|
}
|
|
/** Get the attachment file name */
|
|
static getFileName(response:any, defaultFileName:string) {
|
|
const contentDis = response.headers.get('content-disposition');
|
|
let fileName =defaultFileName;
|
|
if (contentDis && contentDis.indexOf('attachment') !== -1) {
|
|
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
|
|
const matches = filenameRegex.exec(contentDis);
|
|
if (matches != null && matches[1]) {
|
|
fileName = matches[1].replace(/['"]/g, '');
|
|
}
|
|
}
|
|
return fileName;
|
|
}
|
|
} |