读书人

lt;agt;超链接实现下载,普通文件只是打开,

发布时间: 2013-07-01 12:33:04 作者: rapoo

<a>超链接实现下载,普通文件只是打开,特殊后缀的文件无法下载,怎么能让所有的文件都是呈下载,不用打开
<a>超链接实现下载,普通文件只是打开,压缩文件是可以下载,特殊后缀的文件无法下载(注册表注册过后缀的文件能够打开或右键下载),,,,怎么能让所有的文件都是呈下载,不用打开???????

我后台写了个方法


public void ResponseFile(string path)
{
string fileName = HttpContext.Current.Server.UrlEncode(path.Substring(path.LastIndexOf("/") + 1));
try
{
FileInfo info = new FileInfo(HttpContext.Current.Server.MapPath(path));
long fileSize = info.Length;
Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
//不指明Content-Length用Flush的话不会显示下载进度
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(path, 0, fileSize);
Response.Flush();
Response.Close();
}
catch (Exception ex)
{
Response.Write("无法下载文件:" + fileName + ",由于:" + ex.Message + "");
}
}



后台拼接字符串调用估计错的 ,求把我修改下,点击超链接触发后台的方法
]<a href=\"#\" onclick=\"<%=ResponseFile(" + path + ")%)\">" + dr[0].ToString() + "</a>


[解决办法]
<asp:LinkButton ID="LinkButton1" runat="server"></asp:LinkButton>你换成服务器控件就可以了在加一个后台事件
[解决办法]
<a href="down.aspx?path=<%# Server.UrlEncode(Eval("url").ToString())">下载</a>

down.aspx.cs里写下载代码
[解决办法]
引用:
Quote: 引用:

<a href="down.aspx?path=<%# Server.UrlEncode(Eval("url").ToString())">下载</a>

down.aspx.cs里写下载代码


Server.UrlEncode 可以直接传值过去吗?


Server.UrlEncode是转义URL中的特殊字符,如中文,双引号等

读书人网 >asp.net

热点推荐