我们的附件文件被上传到数据库里,在页面上用<a href="DownFile.aspx?id=12">点击下载</a>这种方式供用户下载,这种下载如果用户使用浏览本身下载是没有问题的,可是如果用户电脑上装了迅雷,迅雷会自动拦截,而用迅雷下下来的缺是DownFile.aspx文件,而不是直正的附件文件。
注:DownFile.aspx是一个输出文件流的aspx页面。现有的方案是,当用户浏览带有“<a href="DownFile.aspx?id=12">点击下载</a>”的页面时,把这个链接所对应的文件,在服务器上生成一个物理文件,假设叫作 fujian.rar,那么再把DownFile.aspx?id=12 直接换成 fujian.rar即可,可是这样改动起来有点麻烦,而且影响性能。请问有没有更好的并且快捷的方式来解决这个问题?
注:DownFile.aspx是一个输出文件流的aspx页面。现有的方案是,当用户浏览带有“<a href="DownFile.aspx?id=12">点击下载</a>”的页面时,把这个链接所对应的文件,在服务器上生成一个物理文件,假设叫作 fujian.rar,那么再把DownFile.aspx?id=12 直接换成 fujian.rar即可,可是这样改动起来有点麻烦,而且影响性能。请问有没有更好的并且快捷的方式来解决这个问题?
解决方案 »
- 为何将控件设置为“相对定位”后,拖动控件时,旁边会出现一个蓝色框子?
- Microsoft JScript 运行时错误: 缺少对象
- Panel控件的RenderControl方法问题,百思不得其解,高手们救命啊
- win2000不能访问win2003里面的sql server??
- 程序挑错,源程序如下,有关读Excel表,写入数据库的。
- 谁能帮我看看这段代码为什么不能实例化
- 同一页面服务器控件button1的click事件中触发button2的click事件怎么搞?在线等
- 各位老大有没有比较简单点的购物车代码???发小弟一个吧!!
- 在线等待:为何 btn_subsave.Click += new EventHandler(this.btn_subsave_Click); 不会执行CLICK事件
- 同一session变量在两个页面中取到不同的值,为何?
- asp.net页面设计时背景图片无法正常显示?
- 模板页中添加js验证代码为什么失效了?
http://topic.csdn.net/u/20090715/10/af055c50-eaf4-45c2-8ccd-74fe4410f166.html
{
/// <summary>
/// 讀取文件
/// </summary>
/// <param name="filePath">文件路徑</param>
/// <param name="context">下載頁面</param>
/// <param name="downLoadType">下載類型</param>
private void ReadFile(string filePath,HttpContext context,string downLoadType)
{
try
{
using(FileStream fs= new FileStream(filePath,FileMode.Open,FileAccess.Read, FileShare.Read))
{
string fileName = Path.GetFileName(filePath);
string encodeFileName = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(Path.GetFileNameWithoutExtension(filePath)));
string fileExtension = Path.GetExtension(filePath);
if(fileExtension == ".xls" && encodeFileName.Length > 215)
encodeFileName = encodeFileName.Substring(0,210);
context.Response.ClearContent();
context.Response.Clear();
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.AddHeader("Content-Length", fs.Length.ToString());
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", ""+downLoadType+"; filename=" + encodeFileName + fileExtension+"");
int readLength = 0;
byte[] buffer = new Byte[10240];//緩沖區大小 10k
while(true)
{
if(context.Response.IsClientConnected)
{
readLength = fs.Read(buffer,0,10240);
if(readLength == 0)break;
context.Response.OutputStream.Write(buffer, 0, readLength);
context.Response.Flush();
}
else
{
break;
}
}
context.Response.Close();
}
}
catch(Exception ex)
{
throw ex;
}
if(this.IsDelete){
DeleteFile(filePath);
}
}
private void DeleteFile(string filePath){
try{
if(System.IO.File.Exists(filePath)){
System.IO.File.Delete(filePath);
}
}
catch{}
}
private bool _isDelete;
private bool IsDelete{
get{return this._isDelete;}
}
public void ProcessRequest(HttpContext context)
{
if(context.Request["fn"] != null)
{
string filePath = Utils.Util.DecryptFileName(context.Request.QueryString["fn"].ToString().Trim()); //通过解密得到文件名
this._isDelete = context.Request.QueryString["d"] != null;
ReadFile(filePath,context,context.Request.QueryString["t"] == null ? "attachment" : "inline");//attachment:附件模式;inline在線模式
}
}
public bool IsReusable
{
get{return true;}
}
}這是我常用的下載代碼
用迅雷5可以正確下載
你試試
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment; filename=PrizeInput.xls");
Response.TransmitFile(Server.MapPath("2010.xls"));
Response.End();