C#根据path文件地址进行下载

1:在js文件中写入加密方法

var Comm = {

encode: function (text) {

if (text)

return new Base64().encode(text);

else

return text;

}

}

2:Handler类中返回文件地址:

如:string path = "/Files/审理意见书.doc";

3:在js方法中接收并跳转页面进行下载

function Download(){

  $.post(' ',{action:' '},function(data){

    if(path!=""){

      window.location.href="/Download.aspx?P="+Comm.encode(path);

    }

  });

}

4:在Download.aspx也进行后台代码操作

a:首先写入解密方法

 public static string Decrypt(string text) {

  byte[] bytes = Convert.FromBase64String(text);

  return Encoding.UTF8.GetString(bytes);

}

b:进行文件解析以及下载操作

 protected void Page_Load(object sender, EventArgs e) {

    string path = Decrypt(Parse.ToString(Request["p"]).Replace(" ", "+"));

     string fileName = path.Substring(path.LastIndexOf('/') + 1);

     var filePath = HttpContext.Current.Server.MapPath(path);

     try {

        //判断是否存在此文件

        if (File.Exists(filePath)) {

          FileStream io = File.Open(filePath, FileMode.Open);

          long fileSize = io.Length;

    byte[] filebit = new byte[(int) fileSize];

   io.Read(filebit, 0, (int) fileSize);

    Response.Clear();

   fileName = HttpUtility.UrlEncode(fileName);

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

    Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);

    Response.BinaryWrite(filebit);

   io.Dispose();

   Response.Flush();

   Response.End();

        } else {

     Response.Clear();

     Response.Write("<script type=\"text/javascript\" src=\"/Scripts/jquery-1.10.2.js\"></script><script>$(function () {alert('文件不存在或已被删除', { icon: 0 }, function () { window.close(); }, function () { window.close(); });});</script>");

}

     }catch (Exception ex) {

Response.Clear();

Response.Write("<script type=\"text/javascript\" src=\"/Scripts/jquery-1.10.2.js\"></script> <script>$(function () {alert('" + ex.Message + "', { icon: 0 }, function () { window.close(); }, function () { window.close(); });});</script>");

}

}