【问题标题】:Downloading PDF File in Angular Leaves it Blank在 Angular 中下载 PDF 文件使其空白
【发布时间】:2019-03-22 12:02:51
【问题描述】:

我正在尝试从 API 下载 PDF,但遇到了一个小问题。数据似乎恢复正常;这是它返回的示例:

在 Angular 应用程序中,我使用 ngx-filesaver 包来保存文件。它正在使用不同的 PDF,我对它的控制比在这种情况下要多一些,并且使用 CSV。所以我知道这个包在正确的情况下工作。这是我正在使用的下载文件功能:

getGroup1094BTaxForm(year: string = '2017') {
    return this._http.get(`${this.groupBaseUrl}/taxes/1094/${year}`, { responseType: 'text' }).pipe(
        map((res: any) => {
            const blob = new Blob([res], { type: 'application/pdf' });
            this._fileSaver.save(<any>blob, 'group-1094b.pdf');
            return res;
        })
    );
}

我尝试在GET 请求上将responseType 设置为blobtext,然后在响应返回后创建blob 时,我将其明确设置为application/pdf。当我取出创建 blob 的行并将响应直接传递到 _fileSaver.save 函数时,也会出现同样的问题。

我缺少什么来保存文件而不是空白?我查看了this answer,但没有一个解决方案适合我。

【问题讨论】:

    标签: angular pdf


    【解决方案1】:

    将您的 responseTypetext 更改为 blob

    getGroup1094BTaxForm(year: string = '2017') {
        return this._http.get(`${this.groupBaseUrl}/taxes/1094/${year}`, { responseType: 'blob' }).pipe(
            map((res: any) => {
                const blob = new Blob([res], { type: 'application/pdf' });
                this._fileSaver.save(<any>blob, 'group-1094b.pdf');
                return res;
            })
        );
    }
    

    【讨论】:

    • 对不起,我忘了说得更清楚了。我已经尝试过将responseType 设置为blobtext,两者的结果相同。
    • 你尝试过回复类型arraybuffer
    • 刚刚做了;与其他所有结果相同。
    • Blob([res]更改为Blob([res._body]
    • 打开文件时会报错,"Failed to load PDF Document"。
    猜你喜欢
    • 2020-08-07
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    相关资源
    最近更新 更多