angular - 如何使用 blob 在新选项卡中显示 pdf

标签 angular typescript

我在 TypeScript/Angular 中有这个方法,它生成我的文件

 imprimir() {

            this.service.emitirAdvertencia({ parametros: [{ name: 'CODIGO', value: 880 }] })
            .subscribe((response) => {
                console.log(response);             
                var fileURL = window.URL.createObjectURL(response);                        

                //this not display my pdf document in a new tab.
                window.open(fileURL, '_blank');

                //this display my document pdf, but in current tab
                window.location.href = fileURL; 
            }, error => {
                console.log(error);
            });
        }

这是我的服务
emitirAdvertencia(parametros: Object): any {
        parametros['dbenv'] = ApplicationContext.getInstance().getDbenv();
        parametros['usuario'] = ApplicationContext.getInstance().getUser().codigoUsuario;
        parametros['nome_relatorio'] = 'RelAdvertenciaDB';
        var httpOptions = {

            headers: new HttpHeaders({
                'Authorization': this.localStorage.get('token'),
            }),
            responseType: 'blob' as 'blob',
        };
        return this.http.get(ApplicationContext.URL + '/adiantamento/gerar-relatorio/', httpOptions)
            .map((res) => {
                var report = new Blob([res], { type: 'application/pdf' });
                return report;
            });

就像代码中的注释一样,当我尝试在新选项卡中打开时,不起作用,仅当我在当前选项卡中打开时才有效

如何在新选项卡中打开这个 blob pdf 文件?

最佳答案

To open file in new Tab, you need to create anchor Element in Typescript and add your file url in href attribute of this element.



在我的示例中,服务响应为 data._body对于文件 blob,您可以将其安排为服务的输出响应。
var newTab = true;
var inputData = { parametros: [{ name: 'CODIGO', value: 880 }] };

    this.service.emitirAdvertencia(inputData).subscribe(
                    (data: any) => {
                        var contentType = data.headers._headers.get('content-type')[0];
                        var blob = new Blob([data._body], { type: contentType });
                        var url = window.URL.createObjectURL(blob, { oneTimeOnly: true });

                        //window.open(url, '_blank', '');
                        var anchor = document.createElement('a');
                        anchor.href = url;
                        if (newTab)
                            anchor.target = '_blank';

                        anchor.click();
                    },
                    error => {
                        //TODO
                    },
                    () => { 
                        //TODO 
                    });

关于angular - 如何使用 blob 在新选项卡中显示 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49321675/

相关文章:

具有所选属性的 Angular 选择选项不起作用

javascript - Angular 2 动态表单 - 实现清晰的功能

javascript - 将 Action 链接在一起形成 NgRx (Angular) 的效果

typescript - 模态弹出窗口在分层组件结构中不起作用 angular-bootstrap-md

typescript - 为 DSL 扩展 TypeScript 编译器

angular - Typescript 2.1 加上 Angular 2 编译器静默无法编译项目文件(无输出)

angular - 我怎样才能在 Angular 中解决同样的问题,而 ng-messages 在 AngularJS 中解决了?

Angular : Service Injection vs Typescript Static Methods

javascript - 为多种用途创建 Angular 解析器

angular - 如何以编程方式在angular2中动态创建组件的多个独立实例?