private void ResponDown(string fileName,string filepatch)
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
//Response.Write(writer);
FileStream file = new FileStream(filepatch, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader br = new BinaryReader(file);
Response.AppendHeader("Content-Length", file.Length.ToString());
// StringReader sr = new StringReader(writer.GetStringBuilder().ToString());
long flen =file.Length;
int size = 102400;//每100k同时下载数据
byte[] readdata = new byte[size];//指定缓冲区的大小
if (size > flen) size = Convert.ToInt32(flen);
long fpos = 0;
bool isend = false;
while (!isend)
{
if (Response.IsClientConnected)
{
if ((fpos + size) > flen)
{
size = Convert.ToInt32(flen - fpos);
readdata = new byte[size];
isend = true;
}
if (size > 1)
{
br.Read(readdata, 0, size);//读入一个压缩块
// byte[] re = Encoding.UTF8.GetBytes(readdata, 0, readdata.Length);
Response.BinaryWrite(readdata);
//Response.OutputStream.Write(re, 0, size);
}
fpos += size;
}
else
{
Response.End();
}
}
file.Close();
Response.Flush();
Response.End();
}
给你个下载函数
在page_load事件里加:
string filename = Path.GetFileName( filepath );
Response.Clear();
Response.ContentType = "application/octet-stream ";
Response.AppendHeader( "Content-Disposition ", "attachment; Filename = " + System.Convert.ToChar(34) + filename + System.Convert.ToChar(34) );
Response.Charset = " ";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Flush();
Response.WriteFile( filepath );
filename是显示的文件名,filepath是文件路径
在按钮事件里写:
Response.WriteFile("C:\\abc.txt");
Response.End();
可以直接使用a标签的啊, href跟你文件所在的位置