SUPPORT-8664: fix file downloading

This commit is contained in:
gulnaz 2024-11-02 13:52:09 +03:00
parent a2e611ad10
commit fe27cfa51d
5 changed files with 61 additions and 70 deletions

View file

@ -3,9 +3,10 @@ import {
AnalyticalScope,
Behavior,
Event,
NotNull,
Visible
} from "@webbpm/base-package";
import {ExtractRpcService} from "../../../generated/ru/micord/ervu/service/rpc/ExtractRpcService";
import {HttpClient} from "@angular/common/http";
@AnalyticalScope(AbstractButton)
export class ExtractLoadService extends Behavior {
@ -14,34 +15,34 @@ export class ExtractLoadService extends Behavior {
public successEvent: Event<boolean> = new Event<boolean>();
@Visible("false")
public errorEvent: Event<boolean> = new Event<boolean>();
@NotNull()
public formatRegistry: string;
private button: AbstractButton;
private rpc: ExtractRpcService;
private httpClient: HttpClient;
private onClickFunction: Function;
initialize() {
super.initialize();
this.button = this.getScript(AbstractButton);
this.rpc = this.getScript(ExtractRpcService);
this.httpClient = this.injector.get(HttpClient);
this.onClickFunction = () => {
this.rpc.getExtract()
.then(fileData => {
const newBlob = new Blob([fileData['file']],
{ type: fileData['fileType'] });
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(newBlob);
}
else {
const data = window.URL.createObjectURL(newBlob);
const link = document.createElement("a");
link.href = data;
link.download = fileData['fileName'];
link.click();
URL.revokeObjectURL(data);
link.remove();
this.successEvent.trigger();
}
this.httpClient.get('get-extract/' + this.formatRegistry, {
responseType: 'blob',
observe: 'response'
}).toPromise()
.then((response) => {
const data = window.URL.createObjectURL(response.body);
const link = document.createElement("a");
link.href = data;
const contentDisposition = response.headers.get('Content-Disposition');
const fileNameMatch = contentDisposition.match(/filename\*=?UTF-8''(.+)/i);
link.download = decodeURIComponent(fileNameMatch[1].replace(/\+/g, '%20'));
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(data);
link.remove();
this.successEvent.trigger();
})
.catch(() => {
this.errorEvent.trigger();