From 2ef3d1b2cd9dd994bd12d94ad8d0549e98615daa Mon Sep 17 00:00:00 2001 From: "konstantina.galouni" Date: Thu, 8 Nov 2018 15:27:19 +0000 Subject: [PATCH] searchDownload.component: CSV Download changed for search pages - link ( tag) added in document when service request succeeds. git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@53675 d315682c-612b-4755-9ff5-7f18f6832af3 --- .../searchUtils/searchDownload.component.ts | 31 +++++++++++++++++-- services/reports.service.ts | 2 +- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/searchPages/searchUtils/searchDownload.component.ts b/searchPages/searchUtils/searchDownload.component.ts index e73e093e..7d768a0c 100644 --- a/searchPages/searchUtils/searchDownload.component.ts +++ b/searchPages/searchUtils/searchDownload.component.ts @@ -6,14 +6,17 @@ import {ReportsService} from '../../services/reports.service'; import {ModalLoading} from '../../utils/modal/loading.component'; import {PiwikService} from '../../utils/piwik/piwik.service'; import{EnvProperties} from '../../utils/properties/env-properties'; +import 'rxjs/Rx' ; @Component({ selector: 'search-download', template: ` + (CSV) + { - + this.properties = data.envSpecific; this.csvLimit = data.envSpecific.csvLimit; this.downloadURLAPI = data.envSpecific.csvAPIURL; this.isPiwikEnabled = data.envSpecific.enablePiwikTrack; @@ -77,6 +80,8 @@ export class SearchDownloadComponent { } downloadfile(url:string,filename:string){ + //var newWindow = window.open("", "_parent"); + //var newWindow = window.open(); console.log("Downloading file: "+ url); this.openLoading(); this.setMessageLoading("Downloading CSV file"); @@ -84,17 +89,37 @@ export class SearchDownloadComponent { this._reportsService.downloadCSVFile(url).subscribe( data => { this.closeLoading(); - window.open(window.URL.createObjectURL(data),filename+".csv"); + //window.open(window.URL.createObjectURL(data),filename+".csv"); + console.info("Fill window with data for csv"); + + var url = window.URL.createObjectURL(data); + var a = window.document.createElement('a'); + window.document.body.appendChild(a); + a.setAttribute('style', 'display: none'); + a.href = url; + a.download = filename+".csv"; + a.click(); + window.URL.revokeObjectURL(url); + a.remove(); // remove the element + + //newWindow.location.assign(window.URL.createObjectURL(data)); + + //window.location.href = window.URL.createObjectURL(data); + if(this.isPiwikEnabled && (typeof document !== 'undefined')){ this.downloadFilePiwikSub = this._piwikService.trackDownload(this.properties, url).subscribe(); } }, error => { console.log("Error downloading the file."); + //newWindow.close(); this.closeLoading(); this.confirmOpenCsvError(); }, - () => console.log('Completed file download.') + () => { + console.log('Completed file download.'); + //setTimeout(function(){ newWindow.close(); }, 500); + } ); } diff --git a/services/reports.service.ts b/services/reports.service.ts index ce1a3dd2..9ec08bd5 100644 --- a/services/reports.service.ts +++ b/services/reports.service.ts @@ -15,7 +15,7 @@ export class ReportsService { var headers = new Headers(); headers.append('responseType', 'arraybuffer'); return this.http.get(url) - .map(res => new Blob([res['_body']], { type: 'text/csv' })); + .map(res => new Blob([res['_body']], { type: 'text/csv' })); } getCSVResponse(url: string){ var headers = new Headers();