asp.net里,各种方式汇总-飞外

4 /// param /param 5 public static void DownloadFile(string filePath) 7 string filename = HttpContext.Current.Server.MapPath(filePath); 8 var response = HttpContext.Current.Response; 9 response.ContentType = "application/x-zip-compressed";10 response.AddHeader("Content-Disposition", "attachment;filefilePath" /param 18 public static void DownloadBigFile(string filePath)20 filePath = HttpContext.Current.Server.MapPath(filePath);//路径21 string fileName = Path.GetFileName(filePath);//客户端保存的文件名22 var response = HttpContext.Current.Response;23 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);24 if (fileInfo.Exists == true)26 const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力28 byte[] buffer = new byte[ChunkSize];30 response.Clear();31 System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);32 long dataLengthToRead = iStream.Length;//获取的文件总大小33 response.ContentType = "application/octet-stream";34 response.AddHeader("Content-Disposition", "attachment; filefilePath" /param 51 public static void DownloadFileStream(string filePath)53 filePath = HttpContext.Current.Server.MapPath(filePath);//路径54 string fileName = Path.GetFileName(filePath);//客户端保存的文件名55 var response = HttpContext.Current.Response;56 FileStream fs = new FileStream(filePath, FileMode.Open);58 byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length);60 fs.Close();61 response.ContentType = "application/octet-stream";62 //通知浏览器文件而不是打开63 response.AddHeader("Content-Disposition", "attachment; filetext" /param 73 /// param /param 74 public static void DownloadText(string text , string fileName)76 byte[] bytes = Encoding.Default.GetBytes(text);77 var response = HttpContext.Current.Response;78 response.ContentType = "application/octet-stream";79 //通知浏览器文件而不是打开80 response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));81 response.BinaryWrite(bytes);82 response.Flush();83 response.End();84 }