C# 为什么在一般处理程序无法实现文件下载我在一般处理程序调用如下方法//文件下载到客户端
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();
}执行页面会返回 乱码
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();
}执行页面会返回 乱码
解决方案 »
- 求助用c#编写C/S模式的网上考试系统怎么实现提交后出成绩?
- 关于ToolStripContainer的折叠效果
- 程序提示问题
- C#中怎么确定是文件还是目录
- 很奇怪的问题: C#组件中引用了DirectX DLL, 造成调用它的C++项目调试时会自动中断!
- asp.net(C#) :如何在html代码中使用Cs代码中的函数
- 将一段英文句子切分成单独的单词的正则表达式如何书写?
- 哪位高手能告诉我下关于浏览器兼容能有什么好的解决方法?
- 如何将程序代码转换为这个页面的样式?
- 如何实现隐藏TabControl主体,只显示TabPage的标签,点击标签显示此TabPage?
- 关于C#,问:怎么将图片放在C#项目中,怎么获得被点击的Link Text文本
- 该怎么用asp.net来实现plurk的时间轴??
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();
}
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();
晕,你直接粘贴了?没有修改下?那只是个原理。你必须要加上context才行,而且你不能使用System.Web.HttpContext.Current在加上个参数public void FileDownload(string FileName,HttpContext context)