我在一般处理程序调用如下方法
//文件下载到客户端
public void FileDownload(string FileName)
{
String FullFileName = FileName;// System.Web.HttpContext.Current.Server.MapPath(FileName);
FileInfo DownloadFile = new FileInfo(FullFileName);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=module.zip");
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
执行页面会返回 乱码
8 个解决方案
#1
string filePath = “”;
FileInfo Fi = new FileInfo(filePath);
if (Fi.Exists)
{
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("1.txt",System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
#2
string path = Server.MapPath("~/") + "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.GetEncoding("utf-8")));
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();
也可使用
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.GetEncoding("utf-8")));
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();
也可使用
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
#3
不行啊
在一般处理程序当中就是不行
在一般处理程序当中就是不行
#4
我是说在 【
一般程序】不能用
#5
如果不行,就用ftpwebrequest下载数据
#6
晕,你直接粘贴了?没有修改下?那只是个原理。
你必须 要加上context才行,而且你 不能使用System.Web.HttpContext.Current
在加上个参数public void FileDownload(string FileName,HttpContext context)
#7
当然有加context
#8
这个可以。。我再测试一下
#1
string filePath = “”;
FileInfo Fi = new FileInfo(filePath);
if (Fi.Exists)
{
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("1.txt",System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
#2
string path = Server.MapPath("~/") + "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.GetEncoding("utf-8")));
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();
也可使用
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.GetEncoding("utf-8")));
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();
也可使用
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
#3
不行啊
在一般处理程序当中就是不行
在一般处理程序当中就是不行
#4
我是说在 【
一般程序】不能用
#5
如果不行,就用ftpwebrequest下载数据
#6
晕,你直接粘贴了?没有修改下?那只是个原理。
你必须 要加上context才行,而且你 不能使用System.Web.HttpContext.Current
在加上个参数public void FileDownload(string FileName,HttpContext context)
#7
当然有加context
#8
这个可以。。我再测试一下