using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Net; namespace Bowin.Common { public class FileDownload : IHttpHandler { public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { context.Response.Clear(); string vfilepath = context.Request.QueryString["url"].Trim(); if (string.IsNullOrEmpty(vfilepath)) { context.Response.StatusCode = 404; return; } var isURL = (vfilepath.ToLower().StartsWith(@"http://") || vfilepath.ToLower().StartsWith(@"https://")); string filepath = context.Server.MapPath(isURL ? @"Doc\TempFile\" + Guid.NewGuid() : vfilepath); if (isURL) { WebClient wc = new WebClient(); wc.DownloadFile(vfilepath, filepath); } else if (!System.IO.File.Exists(filepath)) { context.Response.StatusCode = 404; return; } string filename = context.Request.QueryString["filename"] ?? System.IO.Path.GetFileName(filepath); context.Response.ContentType = "application/octet-stream;charset=GB2312"; //context.Response.Charset = "GB2312"; context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + context.Server.UrlEncode(filename)); context.Response.WriteFile(filepath); context.Response.Flush(); } } }