Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Takas0522
Created June 14, 2017 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Takas0522/9922241fdc458d59ad71ff6a229de575 to your computer and use it in GitHub Desktop.
Save Takas0522/9922241fdc458d59ad71ff6a229de575 to your computer and use it in GitHub Desktop.
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