【发布时间】:2012-01-06 00:50:12
【问题描述】:
我有一个 GridView,它具有以下包含文件名的模板字段,当用户单击文件名时,我调用 window.open() 来打开文件。我的问题是我只能将相对路径传递给 window.open()。如果我使用完整路径,则会出现错误。有什么办法可以使用完整路径打开文件吗?谢谢。
<asp:TemplateField HeaderText="FileName">
<ItemTemplate>
<asp:LinkButton ID="lnkFile" runat="server"
Text='<%# Eval("FileName")%>' OnClick="lnkFile_Click">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
添加:文件的实际位置在web.config中定义。
我写了以下 lnkFile_Click()。旧部分将为文件打开一个新窗口,但我无法传递文件的完整路径。新部分将让您选择打开或保存文件。我的问题是,这会导致安全问题吗?
protected void lnkFile_Click(object sender, System.EventArgs e)
{
string fileName = ConfigurationSettings.AppSettings["SPRAttachmentLocation"] + "\\SPR" + sprID + "\\" + ((LinkButton)sender).Text;
if (!File.Exists(fileName))
{
Exception ex = new Exception("Could not find the file, please contact your administrator.");
Response.Write("<p align=\"center\"><br /><br /><br /><br />There has been an Error: <strong>" + ex.Message + "</strong></p>\n");
return;
}
新:
byte[] bts = System.IO.File.ReadAllBytes(fileName);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "");
Response.AddHeader("Content-Length", bts.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bts);
Response.Flush();
Response.End();
旧:
string newWindowUrl = "/SPR_Upload/SPR" + sprID + "/" + ((LinkButton)sender).Text;
string javaScript =
"<script type='text/javascript'>\n" +
"<!--\n" +
"window.open('" + newWindowUrl + "');\n" +
"// -->\n" +
"</script>\n";
Page.ClientScript.RegisterStartupScript(GetType(), "", javaScript);
}
【问题讨论】:
-
此文件是否位于网站/应用程序文件夹下? window.open() 只能通过 URL 寻址 - 相对或绝对,其中绝对以“HTTP”、“HTTPS”等开头。
-
文件的实际位置在web.config中定义。它可能位于远程计算机的网站/应用程序文件夹下。所以你的意思是我不能使用window.open()。有没有其他功能可以使用?
标签: asp.net gridview window.open