asp.net实现文件下载
好久没写东西了 也好久没写代码了 很多东西都生疏了
乘此 写一写关于文件下载的东西 润下手 呵呵
文件下载的几种实现方式:
//第一种实现方法
Response.Clear();
Response.AddHeader("Content-Disposition","attachment;filename=test.txt");
Response.WriteFile(Server.MapPath("test.txt"));
Response.End();
//第二种实现方法
Response.Clear();
Response.AddHeader("Content-Disposition","attachment;filename=test.txt");
FileStream fs=new FileStream(Server.MapPath("test.txt"),FileMode.Open,FileAccess.Read);
BinaryReader reader=new BinaryReader(fs,Encoding.UTF8);
byte[]buffer=new byte[1024];
int recv=reader.Read(buffer,0,1024);
while(recv>0)
{
Response.BinaryWriter(buffer);
recv=reader.Read(buffer,0,1024);
}
Response.Flush();
Response.End();
fs.Close();
reader.Close();
//由于缓冲区设置了1024 最后一个读取可能会读到空白 没解决这个问题
Response.Clear();
Response.AddHeader("Content-Disposition","attachment;filename=test.txt");
Response.WriteFile(Server.MapPath("test.txt"));
Response.End();
//第二种实现方法
Response.Clear();
Response.AddHeader("Content-Disposition","attachment;filename=test.txt");
FileStream fs=new FileStream(Server.MapPath("test.txt"),FileMode.Open,FileAccess.Read);
BinaryReader reader=new BinaryReader(fs,Encoding.UTF8);
byte[]buffer=new byte[1024];
int recv=reader.Read(buffer,0,1024);
while(recv>0)
{
Response.BinaryWriter(buffer);
recv=reader.Read(buffer,0,1024);
}
Response.Flush();
Response.End();
fs.Close();
reader.Close();
//由于缓冲区设置了1024 最后一个读取可能会读到空白 没解决这个问题