私はAngular2プロジェクトでFileSaverを動作させようとしていますが、他の質問からの回答を処理しようとしましたが、見つからないようです。私のサービスでFileSaverとAngular2 - 取得リクエストでファイルが見つかりません
:私のコンポーネントで
@Injectable()
export class DownloadsService {
http: any;
private actionUrl: string;
//constructor(private _http: Http) {
constructor(private jsonp: Jsonp, private _http: Http) {
let hostname = document.location.hostname;
console.log(hostname);
if (hostname === 'localhost') {
this.actionUrl = '/src/assets/rr/spreadsheets/';
} else {
this.actionUrl = '';
}
}
downloadBlaBlasCSV() {
let options = { responseType: ResponseContentType.ArrayBuffer };
console.log(this.actionUrl + 'event_template_2.xlsx');
return this.http
.get(this.actionUrl + 'event_template_2.xlsx', options)
.map((res: Response) => res['_body'])
.catch((err: Response) => Observable.throw(err.json()));
}
}
:私のHTMLで
import { saveAs } from 'file-saver';
downloadBlaBlasCSV() {
this._downloadsService.downloadBlaBlasCSV().subscribe(
(data) => { this.openFileForDownload(data); },
(error: any) => {});
}
openFileForDownload(data: Response) {
var blob = new Blob([data], { type: 'text/xlsx;charset=utf-8' });
saveAs(blob, 'player_template.xlsx');
}
:
<button class="btn btn-primary" (click)="downloadBlaBlasCSV()">Download Spreadsheet Template</button>
そして私は、このエラーが表示されます。
ERROR TypeError: Cannot read property 'get' of undefined
at DownloadsService.webpackJsonp.../../../../../src/app/services/downloads.service.ts.DownloadsService.downloadBlaBlasCSV (downloads.service.ts:32)
at PlayersComponent.webpackJsonp.../../../../../src/app/players/players.component.ts.PlayersComponent.downloadBlaBlasCSV (players.component.ts:27)
at Object.eval [as handleEvent] (PlayersComponent.html:37)
at handleEvent (core.es5.js:11995)
at callWithDebugContext (core.es5.js:13456)
at Object.debugHandleEvent [as handleEvent] (core.es5.js:13044)
at dispatchEvent (core.es5.js:8599)
at core.es5.js:9210
at HTMLButtonElement.<anonymous> (platform-browser.es5.js:2651)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
私は資産フォルダまたは何に正しいURLを作成していないのか分かりませんが、多くの繰り返しを試しても同じエラーが発生します。どんな助けでも大歓迎です。ありがとうございました。
この記事を参照してください。私のactionUrl少し。ありがとう!私はこれをupvoteするのに十分な評判を持っていないが、もしできれば私はするだろう。 – SasquatchShawn
@SasquatchShawnあなたは答えを受け入れることができます。ありがとうございます:-) – alexKhymenko
もう一度@alexKhymenko!私は常にそれを使用しますが、通常は私自身の質問を投稿することなく答えを見つけることができます。とても有難い! – SasquatchShawn