如何在Web浏览器中打开pdf文件?

时间:2016-09-13 09:09:47

标签: c# asp.net pdf

我想显示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);
}

2 个答案:

答案 0 :(得分:2)

Response.ContentType =“Application / pdf”;     Response.TransmitFile(PDFfilepath);

要在新标签或窗口中打开PDF文件,您可以使用以下HTML代码:

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

我希望它可以帮到你。

答案 1 :(得分:0)

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

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