首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Angular http,下载照片功能在Firefox上不起作用

Angular http,下载照片功能在Firefox上不起作用
EN

Stack Overflow用户
提问于 2018-05-28 18:31:41
回答 1查看 151关注 0票数 1

我想知道为什么下面的代码可以在chrome和edge上运行,而不能在火狐上运行。

代码语言:javascript
复制
downloadFile(uri, name){
this.http.get(uri, {responseType: 'blob'})
    .subscribe(res =>{
      var a = document.createElement("a");
      a.href = URL.createObjectURL(res);
      a.download = name;
      a.click();
    }, error => {this.errorHandler.addError(this.constructor.name, error);});}

非常感谢您的帮助,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-28 19:02:41

包括来自http mudule的ResponseContentType以下载该文件。

代码语言:javascript
复制
//service file. Create this method in your service file where you added all services.
import { Headers, Http, RequestOptions, ResponseContentType } from '@angular/http';

download(uri) {
    return this.http.get(uri, { responseType: ResponseContentType.Blob })
    .map((res) => {
        return new Blob([res.blob()], { type: 'application/pdf' })          //change file type here
    })
}

然后使用createObjectURL创建响应的对象

代码语言:javascript
复制
//component.ts
this.service.download(uri)
.subscribe(res => {
    saveAs(res,"filename.ext");
    let fileURL = URL.createObjectURL(res);
    window.open(fileURL);
})

这对所有浏览器都有效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50564347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档