Button的OnClick事件如果实现如()下载文件效果

qq2013 2009-09-18 11:42:43

<asp:Button ID="saveButton" runat="server" Text="下载" OnClick="saveButton_Click" />

protected void saveButton_Click(object sender, EventArgs e)
{
}
中如何实现
<a href='文件路径' ></a>的下载效果!
...全文
813 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2009-09-18
  • 打赏
  • 举报
回复
string path = Server.MapPath("~/") + "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(Name, System.Text.Encoding.GetEncoding("utf-8")) + "." + AttachmentExt);
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();
qq2013 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 smallkonrad 的回复:]
protected void saveButton_Click(object sender, EventArgs e)
    {Response.Redirect(文件路径);
    } 不知道行不
[/Quote]

不是重新打开了一个页面啦!
wjn161 2009-09-18
  • 打赏
  • 举报
回复

Response.Write("<script> window.location.href('xx.doc');</script>");

这样做可行,但不好。期待有更好的方法!
drogon123 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 smallkonrad 的回复:]
protected void saveButton_Click(object sender, EventArgs e)
    {Response.Redirect(文件路径);
    } 不知道行不
[/Quote]

完全可以,不过路径中最后面一定要加上文件的名字.
游北亮 2009-09-18
  • 打赏
  • 举报
回复
protected void saveButton_Click(object sender, EventArgs e)
{
Response.Clear();
//Response.Charset = Encoding.Unicode.ToString();
Response.Charset = "gb-2312";
Response.Buffer = true;
//Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + downloadFilename);

Response.ContentType = "application/unknown";

Response.WriteFile(文件路径);
}
erytbc 2009-09-18
  • 打赏
  • 举报
回复
protected void saveButton_Click(object sender, EventArgs e)
{Response.Redirect(文件路径);
} 不知道行不
qq2013 2009-09-18
  • 打赏
  • 举报
回复
实现直接点击<a href='文件路径' >下载 </a>的效果
qq2013 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sk_aqi 的回复:]
引用 7 楼 wuyq11 的回复:
string path = Server.MapPath("~/") + "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(Name, System.Text.Encoding.GetEncoding("utf-8"))  + "." + AttachmentExt);
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();




要下载时都是调用一个页面,页面page_load写这段代码就ok了
[/Quote]

可以
SK_Aqi 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuyq11 的回复:]
string path = Server.MapPath("~/") + "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(Name, System.Text.Encoding.GetEncoding("utf-8"))  + "." + AttachmentExt);
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();
[/Quote]

要下载时都是调用一个页面,页面page_load写这段代码就ok了
快40的码农 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuyq11 的回复:]
string path = Server.MapPath("~/") + "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(Name, System.Text.Encoding.GetEncoding("utf-8"))  + "." + AttachmentExt);
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();
[/Quote]
这个可以的
nandy249407850 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 smallkonrad 的回复:]
protected void saveButton_Click(object sender, EventArgs e)
    {Response.Redirect(文件路径);
    } 不知道行不
[/Quote]

这个可以
我一直都用这样的

62,249

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧