我们的附件文件被上传到数据库里,在页面上用<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即可,可是这样改动起来有点麻烦,而且影响性能。请问有没有更好的并且快捷的方式来解决这个问题?

解决方案 »

  1.   

    怎么实现下载的
    http://topic.csdn.net/u/20090715/10/af055c50-eaf4-45c2-8ccd-74fe4410f166.html
      

  2.   

    public class DownloadHandler : IHttpHandler
    {
    /// <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可以正確下載
    你試試
      

  3.   

    下载代码,把2010.xls换成要下载的文件的路径即可. 
    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "GB2312";
    Response.AppendHeader("Content-Disposition", "attachment; filename=PrizeInput.xls");
    Response.TransmitFile(Server.MapPath("2010.xls"));
    Response.End();
      

  4.   

    DownFile.aspx 你这个里面的代码没有给出文件名吧