标签:
cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.IO; 6 using System.Security.Cryptography; 7 using System.Text; 8 namespace WebApplication1.handle 9 { 10 /// <summary> 11 /// serverDownLoad 的摘要说明 12 /// </summary> 13 public class serverDownLoad : IHttpHandler 14 { 15 16 public void ProcessRequest(HttpContext context) 17 { 18 19 string id = context.Request.Params["Id"].ToString(); 20 string name = context.Request.Params["name"].ToString(); 21 Console.WriteLine(id); 22 Console.WriteLine(name); 23 using (FileStream fsRead = new FileStream(@"D:\1.xlsx", FileMode.Open)) 24 { 25 //文件读入byte数组中 26 int fsLen = (int)fsRead.Length; 27 byte[] heByte = new byte[fsLen]; 28 int r = fsRead.Read(heByte, 0, heByte.Length); 29 30 //byte数组转换成字符串 31 heByte = System.Text.Encoding.Convert(System.Text.Encoding.GetEncoding("gb2312"), System.Text.Encoding.UTF8, heByte); 32 string myStr = System.Text.Encoding.UTF8.GetString(heByte); 33 string myStr1 = System.Text.Encoding.Unicode.GetString(heByte); 34 Console.WriteLine(myStr); 35 36 //byte数组生成文件的MD5码 37 { 38 MD5 md5 = new MD5CryptoServiceProvider(); 39 byte[] retVal = md5.ComputeHash(heByte); 40 StringBuilder sb = new StringBuilder(); 41 for (int i = 0; i < retVal.Length; i++) 42 { 43 sb.Append(retVal[i].ToString("x2")); 44 } 45 string fileMD5 = sb.ToString(); 46 47 } 48 49 fsRead.Close(); 50 51 //文件下载 52 context.Response.Buffer = true; 53 context.Response.Clear(); 54 context.Response.ContentType = "application/octet-stream"; 55 context.Response.AddHeader("content-disposition", "attachment;filename=1.xlsx"); 56 context.Response.BinaryWrite(heByte); 57 context.Response.Flush(); 58 context.Response.End(); 59 } 60 } 61 62 public bool IsReusable 63 { 64 get 65 { 66 return false; 67 } 68 } 69 } 70 }
使用的时候直接在a标签的href中指向该ashx就可以了,也可以用javascript来实现,href后面添加?Paramsname1=Paramsvalue1&Paramsname2=Paramsvalue2来来实现传参
1 window.location.href = "handle/serverDownLoad.ashx/DownLoad?Id=123&name=fuhai";
获取参数的时候用context.Request.Params[Paramsname]获取
标签:
原文地址:http://www.cnblogs.com/fuhai/p/5122053.html