如何使用c#在新标签页中打开PDF文件?

时间:2015-09-30 09:39:16

标签: c# vb.net pdf c#-4.0

这里我有问题在浏览器的新标签页中打开所选的PDF文件。在下面显示我完成的代码。任何人都可以帮助我...请...

搜索按钮:

protected void Button1_Click(object sender, EventArgs e)
                {
                    ListBox1.Items.Clear();
                    string search = TextBox1.Text;
                    if (TextBox1.Text != "") 
                    {
                        string[] pdffiles = Directory.GetFiles(@"\\192.168.5.10\fbar\REPORT\CLOTHO\H2\REPORT\","*"+ TextBox1.Text + "*.pdf", SearchOption.AllDirectories);

                        foreach (string file in pdffiles)
                        {
              ListBox1.Items.Add(new ListItem(Path.GetFileName(file), file));
                        }
                    }
                    else
                    {
    Response.Write("<script>alert('For this Wafer ID Report is Not Generated');</script>");
                    }
                }

PDF文件打开按钮:

            protected void Button2_Click(object sender, EventArgs e)
            {
                    try
                    {
                    string fileName = ListBox1.SelectedValue;
                    byte[] fileBytes = System.IO.File.ReadAllBytes(fileName);

                    WebClient User = new WebClient();
                    Byte[] FileBuffer = User.DownloadData(fileName);
                    if (FileBuffer != null)
                    {
             Response.ContentType = "application/pdf";
             Response.AddHeader("content-length", FileBuffer.Length.ToString());
             Response.BinaryWrite(FileBuffer);
                    }
                    }
                        catch(Exception ex)
                            {
         Response.Write("<script>alert('PDF File is not Selected');</script>");
                            }
                }
            }

1 个答案:

答案 0 :(得分:1)

你需要注入一些javascript代码:

Response.Write("<script>window.open('" + pdf_filepath + "','_blank')</script>");

这应该有效:D