Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response, Headers} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import 'rxjs/add/observable/of';
5
import 'rxjs/add/operator/do';
6
import 'rxjs/add/operator/share';
7

    
8
@Injectable()
9
export class ReportsService {
10
    // url:string = "http://beta.services.openaire.eu:8480/search/rest/v2/api/publications?format=csv&page=0&size=3&q=(%22test%22)&fq=instancetypename%20exact%20%22Dataset%22";
11
    constructor(private http: Http) {}
12
//text/html
13
    //On the service:
14
   downloadCSVFile(url: string){
15
     var headers = new Headers();
16
     headers.append('responseType', 'arraybuffer');
17
     return this.http.get(url)
18
                .map(res => new Blob([res['_body']], { type: 'text/csv' }));
19
   }
20
   getCSVResponse(url: string){
21
     var headers = new Headers();
22
     headers.append('responseType', 'arraybuffer');
23
     return this.http.get(url)
24
                .map(res => res['_body']);
25
   }
26
   downloadHTMLFile(url: string, info: string){
27
     var headers = new Headers();
28
     headers.append('responseType', 'arraybuffer');
29
     return this.http.get(url)
30
                .map(res => this.addInfo(res, info))
31
                .map(res => new Blob([res['_body']], { type: 'text/html' }))
32
                .do(res => console.log(res))
33
   }
34

    
35
   addInfo(res:any, info:string) {
36
       /*
37
       var para = res.document.createElement("P");                       // Create a <p> element
38
       var t = res.document.createTextNode("This is a paragraph");       // Create a text node
39
       para.appendChild(t);                                          // Append the text to <p>
40
       res.document.body.appendChild(para);
41
       */
42
       res['_body'] = info+res['_body'];
43
       return res;
44
   }
45

    
46
    private handleError (error: Response) {
47
    // in a real world app, we may send the error to some remote logging infrastructure
48
    // instead of just logging it to the console
49
        console.log(error);
50
        return Observable.throw(error  || 'Server error');
51
    }
52

    
53

    
54
}
(12-12/21)