Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,

When I click on the listbox which search a PDF file, it's not opening.
The code is below. Any thoughts? Please help me....

C#
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(file);
      ListBox1.Items.Add(Path.GetFileName(file));
    }
  }
  else
  {
    Response.Write("<script>alert('For this Wafer ID Report is Not Generated');</script>");
  }
}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  string pdffiles = ListBox1.SelectedItem.ToString();

  string.Format("attachment; filename={0}", fileName));

  ProcessStartInfo infoOpenPdf = new ProcessStartInfo();
  infoOpenPdf.FileName = pdffiles;
  infoOpenPdf.Verb = "OPEN";
  // Process.Start(file);
  infoOpenPdf.CreateNoWindow = true;
  infoOpenPdf.WindowStyle = ProcessWindowStyle.Normal;

  Process openPdf = new Process();
  openPdf.StartInfo = infoOpenPdf;
  openPdf.Start();
}
Posted
Comments
CgKumar 29-Sep-15 22:52pm    
Is that the code have any problem?

Ummm...you can't launch a Process in a web application. Well, you can but it launches on the SERVER, not the client browser.

If you're returning a PDF file to the client to display you have to return the file itself, not try and launch it.
 
Share this answer
 
Comments
CgKumar 29-Sep-15 23:49pm    
Thanks for your comment. Did you have any suggestion, how to launch a pdf file from server by using web application? Thanks...
Dave Kreskowiak 30-Sep-15 9:11am    
You cannot launch it from the server! All you do is send the file back in the response and the client will figure out what to do with it.
Agree with the Solution #1.

Well, based on your query what I understood is you want to open a PDF file in browser, (Correct me if I'm wrong please.) right ?

So here's what you can do:
C#
string path = Server.MapPath("mypdfdocument.pdf");
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);

if (buffer != null)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-length", buffer.Length.ToString());
    Response.BinaryWrite(buffer);
}

source from [here][^]

Hope it helps.
-KR
 
Share this answer
 
Comments
CgKumar 13-Oct-15 20:53pm    
Thanks Friend. It is work....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900