Click to See Complete Forum and Search --> : File links not working


mharley
March 9th, 2007, 02:22 PM
I have an application where I read all of the files in a particular directory and display that list in a datalist, along with a hyperlink to each file. For some strange reason, the link will not open despite appearing to be correctly formatted. I've only been able to test this locally so far, so I don't know if that makes a difference. For example, the link will appear as 'file:///C:/Inetpub/wwwroot/app1/files/ImageStore.txt' on the page, but when you click on it nothing will happen. The file information is retrieved from a FileInfo array. I will post both the code and the HTML below. Any help would be greatly appreciated.


DirectoryInfo dFolder = new DirectoryInfo(Server.MapPath("~/files/"));
FileInfo[] fFileArray = dFolder.GetFiles();
DataTable dt = new DataTable();
DataColumn dc = new DataColumn();
DataRow dr;
string sFileName = "", sFileDate = "";

dc.ColumnName = "FileName";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "FileLink";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Added";
dt.Columns.Add(dc);

for (int i = fFileArray.Length - 1; i >= 0; i--)
{
sFileName = fFileArray[i].FullName.Substring(fFileArray[i].FullName.LastIndexOf(@"\")+1);
sFileDate = fFileArray[i].LastWriteTime.ToShortDateString();
dr = dt.NewRow();
dr["FileName"] = sFileName;
dr["FileLink"] = fFileArray[i].FullName.ToString();
dr["Added"] = sFileDate;
dt.Rows.Add(dr);
}


HTML:

<asp: DataList id="dlFiles" runat="server" BorderColor="DarkGray" BorderStyle="None" Width="90%" ShowFooter="False" ShowHeader="False" OnItemCommand="dlFiles_ItemCommand">
<ItemTemplate>
<p style="float: left">
<a href="<%# DataBinder.Eval(Container.DataItem, "FileLink") %>" target="_blank"><%# DataBinder.Eval(Container.DataItem, "FileName") %></a>
</p>
<p style="float: right"><%# DataBinder.Eval(Container.DataItem, "Added") %></p>
</ItemTemplate>
</asp: DataList>

mcmcom
March 9th, 2007, 02:59 PM
you should address the files via a http protocol if this is indeed an asp.net application. The FIle IO path, etc is useful for reading the files in a stream, but to enable downloading or opening of the files you have to use Server.MapPath(thefilePath) so it changes from a file: type to http://server/path/file.ext.

hth,
mcm

mharley
March 12th, 2007, 10:03 AM
I created the file links using Request.ApplicationPath and everything went smoothly. Thanks for your help!