日期:2014-05-17 浏览次数:20621 次
public class downfile : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {            
            if (context.Request.QueryString["file"] != null)
            {
                string path = context.Server.MapPath("/uploadfile/" + context.Request.QueryString["file"]);
                FileInfo fi = new FileInfo(path);
                if (fi.Exists)
                {
                    context.Response.Clear();
                    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + context.Server.UrlEncode(fi.Name));
                    context.Response.AddHeader("Content-Length", fi.Length.ToString());
                    context.Response.ContentType = "application/octet-stream";
                    context.Response.Filter.Close();
                    context.Response.WriteFile(fi.FullName);
                    context.Response.End();
                }
                else
                {
                    context.Response.Status = "404 File Not Found";
                    context.Response.StatusCode = 404;
                    context.Response.StatusDescription = "File Not Found";
                    context.Response.Write("File Not Found");
                    context.Response.End();
                }
            }
        }