下载文件/另存为 .Net 不起作用

发布于 2024-11-13 22:57:51 字数 685 浏览 3 评论 0原文

我有一个基于 jquery lightbox 的屏幕,我想在单击链接按钮时下载文件,但允许我保存的弹出框没有出现...我下面得到的简单测试代码如果放置它就可以工作在表单加载中,但如果我将其放在超链接的服务器端单击事件中,则不会。有什么想法吗?

Response.ContentType = "text/plain";

Response.AppendHeader("Content-Disposition", "attachment; filename=event.txt");
Response.Write("test");
Response.Flush();
Response.End();

通过firebug查看,响应头如下所示,但实际响应只是页面重新加载时的html。

Response Headers
Cache-Control   private
Transfer-Encoding   chunked
Content-Type    text/plain; charset=utf-8
Server  Microsoft-IIS/7.5
X-AspNet-Version    2.0.50727
Content-Disposition attachment; filename=event.txt
X-Powered-By    ASP.NET
Date    Fri, 03 Jun 2011 03:21:25 GMT

I've got a jquery lightbox based screen which I want to download a file when a link button is clicked, but the popup box allowing me to save is not appearing... The simple test code I've got below works if put it in the form load but not if I put it in the server side click event of the Hyperlink. Any ideas why?

Response.ContentType = "text/plain";

Response.AppendHeader("Content-Disposition", "attachment; filename=event.txt");
Response.Write("test");
Response.Flush();
Response.End();

Looking through firebug, the Response Headers are as below, but the actual response is just the html for the page as it reloads.

Response Headers
Cache-Control   private
Transfer-Encoding   chunked
Content-Type    text/plain; charset=utf-8
Server  Microsoft-IIS/7.5
X-AspNet-Version    2.0.50727
Content-Disposition attachment; filename=event.txt
X-Powered-By    ASP.NET
Date    Fri, 03 Jun 2011 03:21:25 GMT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瘫痪情歌 2024-11-20 22:57:51

这是我尝试过的一个实验:

标记:

<p>
    <a href="#" runat="server" ID="lnkDownload" Text="Download Text" onServerClick="DownloadFile">Download</a>
</p>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{

}

protected void DownloadFile(对象发送者,EventArgs eventArgs)
{
Response.ContentType = "文本/纯文本";

Response.AppendHeader("Content-Disposition", "attachment; filename=event.txt");
Response.Write("test");
Response.Flush();
Response.End();

唯一

不同的是我使用 OnServerClick 而不是 OnClick。

这对我在 Firefox 3.6 和 IE9 上有效。

Here's an experiment I tried:

Markup:

<p>
    <a href="#" runat="server" ID="lnkDownload" Text="Download Text" onServerClick="DownloadFile">Download</a>
</p>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{

}

protected void DownloadFile(object sender, EventArgs eventArgs)
{
Response.ContentType = "text/plain";

Response.AppendHeader("Content-Disposition", "attachment; filename=event.txt");
Response.Write("test");
Response.Flush();
Response.End();

}

The only thing different is I used OnServerClick instead of OnClick.

This works for me on Firefox 3.6 and IE9.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文