asp.net 下载文件出问题了
先上代码:
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/x-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
string path = Server.MapPath("download") + "\\" + filename;
if (File.Exists(path))
{
Response.TransmitFile(path);
Response.Flush();
Response.end();
}
else
{
//找不到文件
}
我在download里有一个内容为空的excel,写了这段做代码,运行测试下载文件,结果下载出来文件里面全部是网页里面内容(例如一些控件等等),按常理来说,下载的文件内容应该是空的,为什么会这样子?
下载
------解决方案--------------------Refer:
http://www.cnblogs.com/insus/articles/2003336.html
------解决方案-------------------- Response.TransmitFile(path);
先将文件写入memoryStream
Response.WriteBinary(memoryStream。toarray)
试试
------解决方案-------------------- Response.TransmitFile(path);
Response.Flush();
Response.end();
你这里是输出二进制流,完全看不出
using (FileStream fso = new FileStream(Server.MapPath(DownAdress), FileMode.Open,FileAccess.ReadWrite,FileShare.Inheritable))
{
string[] filename=DownAdress.Split(new char[]{'/'});
int len = Convert.ToInt32(fso.Length);
byte[] FileObj&n