新建download.ashx
- using System; using System.Collections.Generic;
- using System.Linq; using System.Web;
- using System.Web.Services; using System.Net;
- namespace HCLoad.Web
- { /// <summary>
- /// $codebehindclassname$ 的摘要说明 /// </summary>
- public class download : IHttpHandler {
- private long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
- public void ProcessRequest(HttpContext context) {
- //string fileName = "123.jpg";//客户端保存的文件名 String fileName = context.Request.QueryString["filename"];
- string filePath = context.Server.MapPath("Bubble.jpg"); System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
- if (fileInfo.Exists == true) {
- byte[] buffer = new byte[ChunkSize]; context.Response.Clear();
- System.IO.FileStream iStream = System.IO.File.OpenRead(filePath); long dataLengthToRead = iStream.Length;//获得下载文件的总大小
- context.Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开
- context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); while (dataLengthToRead > 0 && context.Response.IsClientConnected)
- { int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
- context.Response.OutputStream.Write(buffer, 0, lengthRead); context.Response.Flush();
- dataLengthToRead = dataLengthToRead - lengthRead; }
- context.Response.Close(); context.Response.End();
- } //context.Response.ContentType = "text/plain";
- //context.Response.Write("Hello World"); }
- public bool IsReusable










