asp.net 问题,一个button实现文件的下载。(附加要求见问题补充)
因为有很多这样的button,所以要添加一个类,然后在button的onclick事件调用该类。求该类的代码,以及button的onclick事件代码。按我的要求实现的话...
因为有很多这样的button,所以要添加一个类,然后在button的onclick事件调用该类。求该类的代码,以及button的onclick事件代码。
按我的要求实现的话,加100分。 展开
按我的要求实现的话,加100分。 展开
2个回答
展开全部
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetButton();
}
}
//将下载按钮的button的id传给SetBtn方法设置下载事件
private void GetButton()
{
SetBtn(Button1.ID);
SetBtn(Button2.ID);
SetBtn(Button3.ID);
SetBtn(Button4.ID);
}
private void SetBtn(string btnID)
{
if (btnID != "")
{
try
{
Button btn = (Button)FindControl(btnID);
btn.Click += new System.EventHandler(this.btn_Click);
}
catch (Exception)
{
//不是button
}
}
}
protected void btn_Click(object sender, EventArgs e)
{
//判断下载的文件是否存在
if (System.IO.File.Exists(Server.MapPath("123.txt")))
{
string filePath = Server.MapPath("123.txt");
FileInfo DownloadFile = new FileInfo(filePath);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
}
{
if (!Page.IsPostBack)
{
GetButton();
}
}
//将下载按钮的button的id传给SetBtn方法设置下载事件
private void GetButton()
{
SetBtn(Button1.ID);
SetBtn(Button2.ID);
SetBtn(Button3.ID);
SetBtn(Button4.ID);
}
private void SetBtn(string btnID)
{
if (btnID != "")
{
try
{
Button btn = (Button)FindControl(btnID);
btn.Click += new System.EventHandler(this.btn_Click);
}
catch (Exception)
{
//不是button
}
}
}
protected void btn_Click(object sender, EventArgs e)
{
//判断下载的文件是否存在
if (System.IO.File.Exists(Server.MapPath("123.txt")))
{
string filePath = Server.MapPath("123.txt");
FileInfo DownloadFile = new FileInfo(filePath);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//下载功能
public void DownLoad(string pathName)
{
string FilePath = Server.MapPath(pathName);
if (System.IO.File.Exists(FilePath))
{
FileInfo file = new FileInfo(FilePath);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码
Response.AddHeader("Content-length", file.Length.ToString());
Response.ContentType = "appliction/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DownLoad("~/文档/学习.txt");
}
看看 这 是不是你想要的功能
public void DownLoad(string pathName)
{
string FilePath = Server.MapPath(pathName);
if (System.IO.File.Exists(FilePath))
{
FileInfo file = new FileInfo(FilePath);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码
Response.AddHeader("Content-length", file.Length.ToString());
Response.ContentType = "appliction/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DownLoad("~/文档/学习.txt");
}
看看 这 是不是你想要的功能
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询