【问题标题】:How to open PDF file in a new tab or window instead of downloading it (using asp.net)?如何在新选项卡或窗口中打开 PDF 文件而不是下载它(使用 asp.net)?
【发布时间】:2012-01-07 19:21:39
【问题描述】:

这是下载文件的代码。

System.IO.FileStream fs = new System.IO.FileStream(Path+"\\"+fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] ar = new byte[(int)fs.Length];
fs.Read(ar, 0, (int)fs.Length);
fs.Close();

Response.AddHeader("content-disposition", "attachment;filename=" + AccNo+".pdf");
Response.ContentType = "application/octectstream";
Response.BinaryWrite(ar);
Response.End();

执行此代码时,它将要求用户打开或保存文件。而不是这个,我需要打开一个新的选项卡或窗口 并显示文件。我怎样才能做到这一点?

注意:

文件不必位于网站文件夹中。它可能位于其他文件夹中。

【问题讨论】:

标签: c# asp.net pdf stream


【解决方案1】:
Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
Response.BinaryWrite(fileContent);

<asp:LinkButton OnClientClick="openInNewTab();" OnClick="CodeBehindMethod".../>

在 JavaScript 中:

<script type="text/javascript">
    function openInNewTab() {
        window.document.forms[0].target = '_blank'; 
        setTimeout(function () { window.document.forms[0].target = ''; }, 0);
    }
</script>

注意重置目标,否则所有其他调用(如Response.Redirect)将在新选项卡中打开,这可能不是您想要的。

【讨论】:

    【解决方案2】:

    您应该看看HttpResponse.TransmitFile

    ,而不是将流加载到字节数组中并将其写入响应流
    Response.ContentType = "Application/pdf";
    Response.TransmitFile(pathtofile);
    

    如果您希望 PDF 在新窗口中打开,您必须在新窗口中打开下载页面,例如:

    <a href="viewpdf.aspx" target="_blank">View PDF</a>
    

    【讨论】:

    • 第一步我有兴趣了解更多。第二步在我的程序中不实用。 bcz 我无法将原始路径发送到它。使用第一步我可以将文件重定向到另一个窗口吗?
    • 您无法从服务器端打开新窗口。您必须在新窗口中打开下载页面,并在该窗口中使用 TransmitFile 将文件返回给用户。
    • 你的链接是什么样的?为什么不能给它添加目标?如果由于某种原因您无法更改 a 标签的来源(例如它是为您生成的),您可以使用 JavaScript 添加它
    • 你可以像这样在服务器端。超链接 hyper = new HyperLink();hyper.NavigateUrl="PDFViewer.aspx"; hyper.Attributes.Add("target","_blank");
    • 不幸的是,这仅在您将实际文件存储在某处时才有效。例如,如果您有一个已生成的字节数组,则没有“路径” - 渲染 Response.TransmitFile 无用。至少这是我遇到的。
    【解决方案3】:

    这可能会有所帮助

    Response.Write("<script>");
    Response.Write("window.open('../Inventory/pages/printableads.pdf', '_newtab');");
    Response.Write("</script>");
    

    【讨论】:

    • 我如何将下载的文件称为这个?那是我的问题。
    • 我应该创建它以供阅读,以后也可以保存它。问题是我不能用文件给出文件的原始名称或路径。所以我必须做上面的步骤来下载它,然后用另一个名字打开它。
    • 你能在服务器上做一个临时副本吗?
    【解决方案4】:

    您必须使用代码创建另一个页面或通用处理程序来生成您的 pdf。然后该事件被触发并且该人被重定向到该页面。

    【讨论】:

      【解决方案5】:

      这里我使用 iTextSharp dll 来生成 PDF 文件。 我想打开 PDF 文件而不是下载它。 所以我正在使用下面的代码,这对我来说很好。 现在pdf文件正在浏览器中打开,正在下载

              Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);
              PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
              pdfDoc.Open();
              Paragraph Text = new Paragraph("Hi , This is Test Content");
              pdfDoc.Add(Text);
              pdfWriter.CloseStream = false;
              pdfDoc.Close();
              Response.Buffer = true;
              Response.ContentType = "application/pdf";
              Response.Cache.SetCacheability(HttpCacheability.NoCache);
              Response.End();
      

      如果你想下载文件, 在 Response.ContentType = "application/pdf";

      之后添加以下行
      Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");   
      

      【讨论】:

        【解决方案6】:

        您可以从 MVC 操作中返回 FileResult。

        *********************MVC动作************

            public FileResult OpenPDF(parameters)
            {
               //code to fetch your pdf byte array
               return File(pdfBytes, "application/pdf");
            }
        

        **************js**************

        使用 formpost 将您的数据发布到行动

            var inputTag = '<input name="paramName" type="text" value="' + payloadString + '">';
            var form = document.createElement("form");
            jQuery(form).attr("id", "pdf-form").attr("name", "pdf-form").attr("class", "pdf-form").attr("target", "_blank");
            jQuery(form).attr("action", "/Controller/OpenPDF").attr("method", "post").attr("enctype", "multipart/form-data");
            jQuery(form).append(inputTag);
            document.body.appendChild(form);
            form.submit();
            document.body.removeChild(form);
            return false;
        

        您需要创建一个表单来发布您的数据、将其附加到您的 dom、发布您的数据并删除您的文档正文的表单。

        但是,表单发布不会仅在 EDGE 浏览器上将数据发布到新标签。但是 get request 可以正常工作,因为它只是打开一个新标签页,其中包含一个包含操作参数查询字符串的 url。

        【讨论】:

          【解决方案7】:

          使用此代码。这就像一个冠军。

          Process process = new Process();
          process.StartInfo.UseShellExecute = true;
          process.StartInfo.FileName = outputPdfFile;
          process.Start();
          

          【讨论】:

          • 我试过这个方法,效果很好,谢谢:)
          • 您的 C# 代码在服务器上运行。因此,Process.Start 在服务器而不是您的计算机上启动一个进程。您根本不可能直接在客户端上启动进程。
          • 谢谢,为我工作。
          猜你喜欢
          • 2019-07-26
          • 2022-06-10
          • 2016-02-15
          • 1970-01-01
          • 2011-10-29
          • 2012-01-28
          相关资源
          最近更新 更多