代码改变世界

以流的方式下载文件,隐藏实际的下载路径

2011-07-11 14:02  12月  阅读(540)  评论(0编辑  收藏  举报

转自: http://dev.mjxy.cn/a-Download-the-file-to-stream-download-hide-the-real-path.aspx

以流的方式下载文件,隐藏实际的下载路径

string path = Server.MapPath("~/UploadFiles/" + "a.doc");

          System.IO.FileInfo file = new System.IO.FileInfo(path);
   
          Response.Clear();
          Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
    

        Response.AddHeader("Content-Length", file.Length.ToString());

        Response.ContentType = "application/octet-stream";

        Response.WriteFile(file.FullName);

        Response.End();

  

public void down(string cc)
    {

   if (cc != "")

   {

  

    string path = System.Web.HttpContext.Current.Server.MapPath(cc);

    System.IO.FileInfo file = new System.IO.FileInfo(path);

    if (file.Exists)

    {

     System.Web.HttpContext.Current.Response.Clear();

     System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

  

     System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());

  

     System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";

  

     System.Web.HttpContext.Current.Response.Filter.Close();

     System.Web.HttpContext.Current.Response.WriteFile(file.FullName);

     System.Web.HttpContext.Current.Response.End();

    }

    else

    {

     System.Web.HttpContext.Current.Response.Write("文件不存在");

     System.Web.HttpContext.Current.Response.End();

    }

    }

  }