Created
June 14, 2017 16:37
-
-
Save Takas0522/9922241fdc458d59ad71ff6a229de575 to your computer and use it in GitHub Desktop.
webapi.ts - CSVImportするやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Inject } from "@angular/core"; | |
import { APP_BASE_HREF } from "@angular/common"; | |
import { Observable } from "rxjs/Observable"; | |
@Injectable() | |
export class WebApi { | |
constructor( | |
@Inject(APP_BASE_HREF) private _baseHref: string | |
) { } | |
rootEndPoint = () => { | |
const endP = this._baseHref; | |
let returnString = ""; | |
if (endP + "" === "/") { | |
returnString = endP; | |
} else { | |
returnString = endP + "/"; | |
} | |
return returnString; | |
} | |
downloadFileGet(hasError: boolean): Observable<Object[]> { | |
return Observable.create(observer => { | |
let xhr = new XMLHttpRequest(); | |
xhr.open("GET", this.rootEndPoint() + "api/CsvExport/?hasError=" + hasError, true); | |
xhr.setRequestHeader("Accept", "application/json"); | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState === 2) { | |
if (xhr.status === 200) { | |
xhr.responseType = "blob"; | |
} | |
} | |
if (xhr.readyState === 4) { | |
if (xhr.status === 200) { | |
var contentType = "text/csv"; | |
var blob = new Blob([xhr.response], { type: contentType }); | |
observer.next(blob); | |
observer.complete(); | |
} else { | |
observer.error(xhr); | |
} | |
} | |
} | |
xhr.send(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment