Project

General

Profile

« Previous | Next » 

Revision 53675

searchDownload.component: CSV Download changed for search pages - link (<a> tag) added in document when service request succeeds.

View differences:

modules/uoa-services-library/trunk/ng-openaire-library/src/app/services/reports.service.ts
15 15
     var headers = new Headers();
16 16
     headers.append('responseType', 'arraybuffer');
17 17
     return this.http.get(url)
18
               .map(res => new Blob([res['_body']], { type: 'text/csv' }));
18
                .map(res => new Blob([res['_body']], { type: 'text/csv' }));
19 19
   }
20 20
   getCSVResponse(url: string){
21 21
     var headers = new Headers();
modules/uoa-services-library/trunk/ng-openaire-library/src/app/searchPages/searchUtils/searchDownload.component.ts
6 6
import {ModalLoading} from '../../utils/modal/loading.component';
7 7
import {PiwikService} from '../../utils/piwik/piwik.service';
8 8
import{EnvProperties} from '../../utils/properties/env-properties';
9
import 'rxjs/Rx' ;
9 10

  
10 11
@Component({
11 12
    selector: 'search-download',
12 13
    template: `
13 14
        <a class="uk-margin-large-right" *ngIf="totalResults > 0 && totalResults <= csvLimit">
14 15
            <span class="clickable" (click)="downloadfile(downloadURLAPI+'?type='+type+'&format=csv'+csvParams,type+'-report-'+totalResults)">
16
            <!--a [href]="downloadURLAPI+'s'+'?type='+type+'&format=csv'+csvParams" target="_blank"-->
15 17
                <span aria-hidden="true" class="glyphicon glyphicon-download"></span>
16 18
                <span  class="uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="download" ratio="1"><polyline fill="none" stroke="#000" points="14,10 9.5,14.5 5,10"></polyline><rect x="3" y="17" width="13" height="1"></rect><line fill="none" stroke="#000" x1="9.5" y1="13.91" x2="9.5" y2="3"></line></svg></span> (CSV)
19
            <!--/a-->
17 20
            </span>
18 21
        </a>
19 22
        <span class="uk-margin-large-right cursor-not-allowed" *ngIf="totalResults > csvLimit"
......
49 52
    ngOnInit() {
50 53
      this.route.data
51 54
        .subscribe((data: { envSpecific: EnvProperties }) => {
52

  
55
          this.properties = data.envSpecific;
53 56
          this.csvLimit = data.envSpecific.csvLimit;
54 57
          this.downloadURLAPI = data.envSpecific.csvAPIURL;
55 58
          this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
......
77 80

  
78 81
    }
79 82
    downloadfile(url:string,filename:string){
83
      //var newWindow = window.open("", "_parent");
84
      //var newWindow = window.open();
80 85
      console.log("Downloading file: "+ url);
81 86
      this.openLoading();
82 87
      this.setMessageLoading("Downloading CSV file");
......
84 89
      this._reportsService.downloadCSVFile(url).subscribe(
85 90
          data => {
86 91
              this.closeLoading();
87
              window.open(window.URL.createObjectURL(data),filename+".csv");
92
              //window.open(window.URL.createObjectURL(data),filename+".csv");
93
              console.info("Fill window with data for csv");
94

  
95
              var url = window.URL.createObjectURL(data);
96
              var a = window.document.createElement('a');
97
              window.document.body.appendChild(a);
98
              a.setAttribute('style', 'display: none');
99
              a.href = url;
100
              a.download = filename+".csv";
101
              a.click();
102
              window.URL.revokeObjectURL(url);
103
              a.remove(); // remove the element
104

  
105
              //newWindow.location.assign(window.URL.createObjectURL(data));
106

  
107
              //window.location.href = window.URL.createObjectURL(data);
108

  
88 109
              if(this.isPiwikEnabled && (typeof document !== 'undefined')){
89 110
                this.downloadFilePiwikSub = this._piwikService.trackDownload(this.properties, url).subscribe();
90 111
              }
91 112
          },
92 113
          error => {
93 114
            console.log("Error downloading the file.");
115
            //newWindow.close();
94 116
            this.closeLoading();
95 117
            this.confirmOpenCsvError();
96 118
          },
97
          () => console.log('Completed file download.')
119
          () => {
120
            console.log('Completed file download.');
121
            //setTimeout(function(){ newWindow.close(); }, 500);
122
          }
98 123
      );
99 124
    }
100 125

  

Also available in: Unified diff