This is related to another post, I had trouble setting the absolute path of a URL from the code-behind in a webform, but I was able t开发者_Python百科o resolve it. I have a pdf file on a server share that I would like the link to point to, the URL is as follows:
file://///myServer/share/MyFile.pdf
I verified the pdf opens correctly by adding it manually to the address bar in my browser. After I type it in, the pdf opens in my browser as expected. When I try to click on the link in my web form, however, nothing happens. I compared the URL strings from the link and what I type in my browser, both are the same. Why won't the anchor redirect to the location specified, but typing it in manually in the address bar does? Any help is appreciated.
If your page is served by a remote server but contains links to local files, most modern browsers will refuse to navigate those links for security reasons.
You will probably have to serve your linked content from the server instead of the client machine to make that work.
have you tried
Server.MapPath
http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx
Here is some C# code that I use to scan the contents of a directory and build a link to the files that are inside a folder on the server. It sounds like you only have one file, but it will still work fine and you can tweak it if needed.
Obviously this is geared toward building a list of links out of the files inside a folder and it's definitely a bit of overkill for your example, but maybe it will give you some ideas.
DirectoryInfo di = default(DirectoryInfo);
FileInfo[] files = null;
DataTable dt = new DataTable();
DataRow dr = null;
System.DateTime filedate = default(System.DateTime);
di = new DirectoryInfo(Server.MapPath("~/forms"));
files = di.GetFiles();
dt.Columns.Add("name");
dt.Columns.Add("filepath");
dt.Columns.Add("filedate");
foreach (FileInfo inf in files)
{
filedate = inf.LastWriteTime;
dr = dt.NewRow();
dr["name"] = inf.Name;
dr["filepath"] = inf.FullName;
dr["filedate"] = String.Format("{0:MM/dd/yyyy}", filedate);
dt.Rows.Add(dr);
}
DataList1.DataSource = dt;
DataList1.DataBind();
And on your aspx page:
<asp:DataList ID="DataList1" runat="server" RepeatColumns="1" GridLines="none">
<HeaderTemplate>
<table>
<tr>
<td style="width: 450px">
<asp:Label ID="label1" runat="server" Text="Form Name" Font-Bold="true"></asp:Label>
</td>
<td>
<asp:Label ID="label2" runat="server" Text="Creation Date" Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td style="width: 446px">
<a target="_blank" href='http://yourwebserverpath.com/forms/<%# DataBinder.Eval(Container.DataItem, "name") %>'>
<%# DataBinder.Eval(Container.DataItem, "name") %></a>
</td>
<td style="padding: 0 0 0 5px">
<%#DataBinder.Eval(Container.DataItem, "filedate")%>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<table>
<tr>
<td style="width: 446px">
</td>
<td style="padding: 0 0 0 5px">
</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
精彩评论