[英]Visual Studio 2010 ASP.net c# Download file for hyper link at runtime not working
I have a Visual Studio 2010 ASP.net web site using c#.我有一个使用 c# 的 Visual Studio 2010 ASP.net 网站。
I can display all files for a specific folder location at runtime on a Web Page:-我可以在运行时在网页上显示特定文件夹位置的所有文件:-
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Downloads"));
int i = 0;
foreach (FileInfo fi in di.GetFiles())
{
HyperLink HL = new HyperLink();
HL.ID = "HyperLink" + i++;
HL.Text = fi.Name;
HL.NavigateUrl = "FileDownloads.aspx?file=" + fi.Name;
Page.Controls.Add(HL);
Page.Controls.Add(new LiteralControl("<br/>"));
}
}
The above code displays the files as a hyperlink on the Web Page but when I click on any link the page seems to refresh and not download the specific file?上面的代码将文件显示为网页上的超链接,但是当我单击任何链接时,页面似乎刷新而不下载特定文件?
Can I assign a password to the links?我可以为链接指定密码吗?
Would a Android phone user experience the same behaviors as a windows web browser user? Android 手机用户会遇到与 Windows 网络浏览器用户相同的行为吗?
Any examples would be welcomed.任何例子都会受到欢迎。
tia蒂亚
As a work around I now use LinkButtons.作为一种解决方法,我现在使用 LinkButtons。
First I manually placed a number of PlaceHolder controls on the web page.首先,我在网页上手动放置了一些 PlaceHolder 控件。
On the Page Load event I then search a specific web site folder for any files.在页面加载事件中,我然后在特定的网站文件夹中搜索任何文件。 If any file found then create a LinkButton and assign the method TransmitFile to the LinkButton Command event.
如果找到任何文件,则创建一个 LinkButton 并将方法 TransmitFile 分配给 LinkButton 命令事件。
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Downloads"));
int i = 0;
foreach (FileInfo fi in di.GetFiles())
{
LinkButton LB = new LinkButton();
LB.ID = "LinkButton" + i++;
LB.Text = fi.Name;
LB.CommandName = Convert.ToString(i);
LB.Command += new CommandEventHandler(TransmitFile);
PlaceHolder ph = (PlaceHolder)Page.FindControl("PlaceHolder" + Convert.ToString(i));
if (ph != null)
{
ph.Controls.Add(LB);
}
}
}
protected void TransmitFile(object sender, CommandEventArgs e)
{
LinkButton lnk = sender as LinkButton;
var filePath = "~/Downloads/" + lnk.Text;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + lnk.Text);
Response.TransmitFile(filePath);
Response.End();
}
For the Password side I've opted to have a login page prior to the download page.对于密码方面,我选择在下载页面之前有一个登录页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.