如何在asp.net中实现点击下载按钮进行文件下载?
发布网友
发布时间:2022-04-22 23:48
我来回答
共4个回答
热心网友
时间:2022-04-27 10:14
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();
}
给你个下载函数追问你说的太深奥我不懂哎。。。。。。。。
热心网友
时间:2022-04-27 11:32
在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是文件路径
热心网友
时间:2022-04-27 13:06
在按钮事件里写:
Response.WriteFile("C:\\abc.txt");
Response.End();
热心网友
时间:2022-04-27 14:58
可以直接使用a标签的啊, href跟你文件所在的位置