我在創建Angular2服務從服務器下載文件時遇到問題。我有表格,每個記錄都有單個文件。點擊一些唱片,叫下載方法:從Angular2服務下載文件不起作用。
download(r: FileObject) {
this.repositoryService.download(r).then((result) => {
let blob = new Blob([result.blob()], { type: result.headers.get('content-type') });
let url = window.URL.createObjectURL(blob);
window.open(url);
});
}
哪裏有合適的文件對象當前文件下載。
在我RepositoryService我寫道:
download(data: FileObject) {
let options: RequestOptions = this.getParams(data.name);
return this.http.get(`/repository/file`, options)
.toPromise()
.then((response) => {
this.authService.refreshSession();
return response;
})
.catch(this.handleError);
}
getParams是一個簡單的方法來在URL傳遞文件名作爲parametr:
private getParams(filePath: string): RequestOptions {
let params: URLSearchParams = new URLSearchParams();
params.set('filePath', filePath);
let requestOptions = new RequestOptions({responseType: ResponseContentType.Blob});
requestOptions.params = params;
return requestOptions;
}
我登錄所有的方法,我看到該文件當我嘗試下載它們時,它會被正確返回,但它不能保存。
當我嘗試下載jpg文件時,屏幕刷新並沒有任何保存。 當我嘗試下載XML文件,不帶擴展名陌生文件保存:
任何人都可以幫助我,告訴我,我做錯了什麼?