Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I m trying to display the pdf files in list box and on clicking on the file (on selected index changed) i want to open that particular file.
But i m nt able to do so as i m getting the single file(Clicked first) on click of any link.
C#
string var = txtpan.Text;
ListItem list = new ListItem();
DirectoryInfo dinfo = new DirectoryInfo(@"C:\vrishali\PDFs\");
/* get files from the directory as a files collection */
FileInfo[] finfo = dinfo.GetFiles(string.Format("{0}*.pdf",var));
lstpdfs.Items.Clear();
//files=Directory.GetFiles(@"C:\folder\PDFs\",string.Format("{0}*.pdf",var));
for (int i = 0; i < finfo.Length; i++)
{            
  list.Value = finfo[i].ToString();
  lstpdfs.Items.Add(list.Value);
}

C#
protected void lstpdfs_SelectedIndexChanged(object sender, EventArgs e)
{
string filePath = @"C:\folder\PDFs\"+ lstpdfs.SelectedValue;
Session["pdfFileName"] = filePath ;
Response.Write("<script type='text/javascript'>detailedresults=window.open('../PdfDisplay.aspx');</script>"); }


ON PDFDISPLAY.ASPX I have coded as
C#
string path = Session["pdfFileName"].ToString();
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
Response.ClearContent();
Response.Flush();
Response.Close();
}

This is my code. I am not able to clear the response stream as i am getting first clicked file on click of any other link.
please help
Posted
Updated 27-Jun-12 20:31pm
v2
Comments
Herman<T>.Instance 28-Jun-12 3:06am    
set the content-disposition header too

1 solution

Might be a caching issue. You could try setting the response content expires or caching settings or you can try and append a random number or Guid to the querystring e.g. ?c={Guid.NewGuid()} so that the browser will have to request for new content (might be needed for the PdfDisplay.aspx in your javascript since that url doesn't change I imagine). Not sure if that is what it is or if you have an issue with the list containing the wrong values. I imagine it is the browser caching though.

Also, looking at your selectedindex changed, I would imagine instead of response.write you might want a placeholder that you can update that javascript into instead of just sending back to the response stream. Have you looked at your html source after a few postbacks and make sure it isn't just adding the javascript again and again?
 
Share this answer
 
Comments
Member 8683058 10-Jul-12 4:25am    
Thanks Trak4Net769 for ur soln. it helped

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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