Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
webapi.ts - CSVImportするやつ
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