我有这个代码从服务器下载文件,它适用于大多数文件,除了SQLite * .db文件
download(node){
this.service.downloadFile(node.data.path)
.toPromise()
.then(response => this.saveToFileSystem(response, node.data.name));
}
private saveToFileSystem(response, filename) {
const blob = new Blob([response._body], { /*type: 'application/octet-stream'*/ });
saveAs(blob ,filename);
}
* .db文件正在下载到我的计算机上,但在打开时会得到:
我尝试了两种方式,有/无:输入:'application / octet-stream'
答案 0 :(得分:1)
当我将服务中的get调用更新为this.http.get(url, {responseType:ResponseContentType.Blob} )时工作:
downloadFile(path){
var url = this.fileBrowseUrl + "download?path=" + path.replace(/\\/g,"%5C");
return this.http.get(url ,{responseType: ResponseContentType.Blob})
.do(/*this.showSpinner = true*/)
.catch((response: Response) => this.errorHandler(response) );
}
答案 1 :(得分:0)
这就是我下载文件的方式,也许这会对您有所帮助: 成分:
this._service.export(column, date)
.subscribe(
(data) => { this.openFileForDownload(data);},
error => { this.errorMessage = <any>error; },
() => { });
openFileForDownload(data: HttpResponse<any>) {
let filename = data.headers.get('filename');
saveAs(data.body, filename);}
服务:
export(column: string, date: Date) {
let params = new HttpParams()
.append('column', column)
.append('date', date ? date.toISOString() : '')
let headers = new HttpHeaders({ 'Content-Type': 'application/JSON' });
return this._http.get<any>(url, { headers: headers, params: params, responseType: 'blob' as 'json', observe: 'response' as 'body' });
}