can anyone point me to any articles on how i can read back .pdf files from a folder called Reports on my server, an then list them on my Webpage, so that when you click them they open up the .pdf file in a new browser window??
Thanks in advance!
If you mean how can you list all the reports in a folder and then hyperlink to it, then here is a solution.' Get file informationDim parentDirAs DirectoryInfoDim childFilesAs FileInfo()Try parentDir =New DirectoryInfo(Server.MapPath(folderPath)) childFiles = parentDir.GetFiles()Catch excAs Exception StatusMessage.Text = exc.Message StatusMessage.Visible =TrueEnd TryDim childFileAs FileInfoFor Each childFileIn childFilesDim linkItemAs New HyperLink()With linkItem .Text = childFile.Name .Target ="_blank" .NavigateUrl ="fullpathOfFile"End WithNext
that sounds about right for what i am trying to do, but im pretty crap at vb.net, in fact its nonexistant to say the least. anyway you could translate it to c# please? is the below correct:
DirectoryInfo parentDir = new DirectoryInfo();
FileInfo childFiles = new FileInfo();
try
{
parentDir = Server.MapPath("C:\Inetpub\wwwroot\Database\Reports\");
childFiles = parentDir.GetFiles();
}
catch (ex As Exception)
{
textBox1.text = ex.Message();
textBox1.visible = true;
}
FileInfo childFile = new FileInfo();
foreach(childFile in childFiles)
{
HyperLink linkItem = new HyperLink();
linkItem.text = childFile.Name;
linkItem.Targer = "_blank";
linkItem.NavigateUrl = "####"
}
Next
not sure if this is correct or not. any help appreciated. thanks!
FileInfo childFiles = new FileInfo();
Should be:
FileInfo[] childFiles;
//because this is an array of files and not a single file
Otherwise everything looks fine. Also the argument passed for MapPath method is the virtual path and not the actual path. This method takes in the virtual path and gives you the actual path. If you want to specify a directory then use DirectoryInfo(string path). Try out your code, if you run into any problems reply to the post.
Best of luck.
Thanks for replying. get the following error:
Type and identifier are both required in a foreach statement
my code is below:
private
void LnkBtn_ViewRpt_Click(object sender, System.EventArgs e){
//this will show all the reports presently in the system by reading
//through all of the pdf report files in the folder on the drive.
System.IO.DirectoryInfo parentDir;
System.IO.FileInfo[] childFiles;
try
{
parentDir =new System.IO.DirectoryInfo("C:\\Inetpub\\wwwroot\\CalendarBCP\\Reports_PDF\\");
childFiles = parentDir.GetFiles();
}
catch (Exception ex)
{
Label6.Text = ex.Message;
}
System.IO.FileInfo[] childFile;
foreach(childFilein childFiles)
{
HyperLink linkItem =new HyperLink();
linkItem.Text = childFile.Name;
linkItem.Target = "_blank";
linkItem.NavigateUrl = "#";
}
}
Any help is greatly appreciated. Thanks!
I have found another way:
private
void LnkBtn_ViewRpt_Click(object sender, System.EventArgs e){
//this will show all the reports presently in the system by reading
//through all of the pdf report files in the folder on the drive.
string location = "C:\\Inetpub\\wwwroot\\CalendarBCP\\Reports_PDF\\";
System.IO.DirectoryInfo dir =new System.IO.DirectoryInfo(location);
foreach (System.IO.FileInfo fin dir.GetFiles())
{
//load items
//HyperLink linkItem = new HyperLink();
//linkItem.Text = f.Name;
//linkItem.Target = "_blank";
//linkItem.NavigateUrl = "#";
ListBox1.Items.Add(f.Name);
//ListBox1.Items.Add(linkItem);
}
}
I can get it to display the items from the folder into a listbox, but i dont know how to add a linked item to a listbox. im not sure if this is possible or not. ideally I already have datagrid on the form, is it not possible to add the items to the datagrid and the somehow create a link to each document to be opened in a new browser window.
Any help appreciated. Thanks in advance!
Glad you figured out a solution. Unfortunately you cannot have a hyperlink as a ListItem in a ListBox (unless you are going to use third party items. A good example ishttp://www.easylistbox.com by Peter Brunone. I have never used it. You might get more products, if you search the control gallery of this site as well as search in google).
Other options are to use any of the other data-presentation controls and use the template to achieve what you want or create a place holder (panel, placeholder, table etc) and create the hyperlinks and add to the control's collection of the container.
Hope this answers your questions.
0 comments:
Post a Comment