public void ProcessRequest(HttpContext context)
{
string url = context.Request.QueryString["url"];
try
{
url = HttpContext.Current.Server.UrlEncode(url.Trim());
downloadfile(url);
}
catch (ArgumentException ex)
{
return;
} } public void downloadfile(string s_fileName)
{
HttpContext.Current.Response.ContentType = "application/ms-download";
s_fileName = HttpContext.Current.Server.UrlDecode(s_fileName);
string s_path = HttpContext.Current.Server.MapPath("~/")+@"download\excel\" + s_fileName;
System.IO.FileInfo file = new System.IO.FileInfo(s_path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
}当文件名 为中文时,有的用户正常,有的用户下载会报异常
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------路径中具有非法字符。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentException: 路径中具有非法字符。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。 堆栈跟踪:
[ArgumentException: 路径中具有非法字符。]
打印出s_path看是什么
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
string urlName = HttpUtility.UrlEncode(s_fileName, System.Text.Encoding.UTF8);
Response.AddHeader("Content-Disposition", "attachment;filename=" + urlName);
Response.AddHeader("Content-Length", info.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(info.FullName);
Response.Flush();
Response.End();