有没有办法强制浏览器通过JavaScript / TypeScript打开下载窗口。我想打开下载窗口,因为大多数用户直接下载到下载文件夹,我想为用户提供重命名文件和选择下载文件夹的可能性。
我启用了这样的下载:
const json = JSON.stringify(data);
const blob = new Blob([json], {type: 'application/json'});
const url = URL.createObjectURL(blob);
const a = document.getElementById('export');
a.download = 'export.json';
a.href = url;
答案 0 :(得分:0)
相同的完整博文:Download File in Angular Js2 Application
你的代码中缺少这两行:
var url= window.URL.createObjectURL(b);//add this line
window.open(url);//add this line for download
我建议尝试这样:
constructor(private http:Http)
{}
DownloadFile():void
{
this.getFile("http://localhost:1800/api/demo/GetTestFile")
.subscribe(fileData =>
{
let b:any = new Blob([fileData], { type: 'application/zip' });
var url= window.URL.createObjectURL(b);//add this line
window.open(url);//add this line for download
}
);
}
public getFile(path: string):Observable<any>{
let options = new RequestOptions({responseType: ResponseContentType.Blob});
return this.http.get(path, options)
.map((response: Response) => <Blob>response.blob()) ;
}