【问题标题】:How to open a pdf file in the web browser?如何在网络浏览器中打开pdf文件?
【发布时间】:2017-01-20 19:55:15
【问题描述】:

我想显示一个 pdf。我当前的代码使用 pdf 阅读器显示。但我想在浏览器的单独选项卡中打开它。我该怎么做?我在网页中有一个链接按钮。我已经在 onClick 方法中设置了这个。如何使用后端代码打开它? (不使用 aspx 中的链接)

这是我的代码

string name = ddlAppealList.SelectedValue.ToString();
int refNo = Convert.ToInt32(name);
string FilePath = Server.MapPath("~/filesPDF/" + refNo + ".pdf");
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(FilePath);
if (FileBuffer != null)
{
     Response.ContentType = "application/pdf";
     Response.AddHeader("content-length", FileBuffer.Length.ToString());
     Response.BinaryWrite(FileBuffer);
}

【问题讨论】:

标签: c# asp.net pdf


【解决方案1】:

几周前我遇到了类似的情况。受this answer启发的这段代码帮助我解决了这个问题:

Response.AppendHeader("Content-Disposition", new System.Net.Mime.ContentDisposition { Inline = true, FileName = "pdfname.pdf" }.ToString());

【讨论】:

    【解决方案2】:

    Response.ContentType = "应用程序/pdf"; Response.TransmitFile(PDFfilepath);

    要在新选项卡或窗口中打开 PDF 文件,您可以使用以下 html 代码:

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

    希望对你有帮助。

    【讨论】:

    • @Ashouri,此代码无法按我的预期工作。它打开一个选项卡。但再次自动关闭它,并像往常一样要求打开 pdf 文件。如何解决?
    猜你喜欢
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多