如何在asp.net中实现点击下载按钮进行文件下载?
展开全部
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();
}
给你个下载函数
{
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是文件路径
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();
Response.WriteFile("C:\\abc.txt");
Response.End();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以直接使用a标签的啊, href跟你文件所在的位置
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询