首页 > 解决方案 > 如何打开PDF文件而不是在浏览器中以角度下载它?

问题描述

我在 Angular 4 中有前端,在 Dot net 有后端,我有一个 URL,当我在浏览器中点击该 URL 时,正在下载 PDF,但我想在浏览器中显示它。

我通过 get 调用收到了该 URL,然后我在新窗口中打开了该 URL,但它总是被下载。

这是我尝试过的:

ts文件代码:

this.http.get(this.pdfUrl, { params: { data: JSON.stringify(prospect) }})
                .subscribe(data => {

                    var strWindowFeatures = "location=yes,height=570,width=520,scrollbars=yes,status=yes";
                    window.open(data.url, "_blank", strWindowFeatures);
                },
                    error => {
                        console.log(error);
                    })
        ]).then(function (result) {
            if (result) {
                console.log("ok");
            }
          });

在此 get 调用的响应中,我收到加密格式的 PDF 文件数据和下载 PDF 的 URL。

我的控制器代码:

                HtmlToPdf converter = new HtmlToPdf();
                var rootDir = this.environment.ContentRootPath;

                rootDir = this.environment.ContentRootPath + "/bin/Debug/netcoreapp2.0";

                 IHTMLTemplateProvider proposalProvider = new ProposalProvider();
                proposalProvider.LoadDocument(rootDir + "/templates/Proposal.html");

                proposalProvider.ParseDocument();

                string notificationContent = await proposalProvider.GetDocumentInText().ConfigureAwait(false);
                SelectPdf.PdfDocument doc = converter.ConvertHtmlString(notificationContent);

                byte[] pdfData = doc.Save();
                doc.Close();

                return File(pdfData, "application/pdf", "document.pdf");

标签: asp.netangularhtml-to-pdf

解决方案


推荐阅读