【问题标题】:Show a message after sending a file to download发送要下载的文件后显示消息
【发布时间】:2016-07-27 15:32:44
【问题描述】:

在发送要下载的文件后,我想更新我的 aspx 页面上的某些内容。 (之前显示的一些错误消息。)。我认为这是不可能的,但您能提供一个解决方案吗?

这里是发送文件下载的代码:

  Response.ContentType = "Application/zip";
  Response.AddHeader("Content-Disposition", "attachment; filename=" + e.CommandArgument);
  Response.BinaryWrite(fileStream.ToArray());
  Response.Flush();
  Response.Close();
  Response.End();

编辑澄清:我也认为没有合乎逻辑的解决方案。但是,可能有一个我不知道的 Javascript 技巧。

【问题讨论】:

  • Response.Write("文件已发送给您");
  • @inquisitive_mind 他已经在用文件写响应。他的意思是他也想更新页面。他无法同时响应文件和更新的页面。
  • 您需要使用javascript,进行ajax调用并在成功函数中更新您的页面
  • 没错,没有合乎逻辑的解决方案。但是,可能有一个我不知道的 Javascript 技巧。

标签: c# asp.net response httpresponse httpwebresponse


【解决方案1】:

这是我能想象到的最简单的方法。

<a href="Default.aspx?download=1"
    onclick="javascript:document.write('the file was downloaded');" >
    Click here to Download
</a>

在我后面的代码中

protected void Page_Load(object sender, EventArgs e)
{
    if(Request["download"]=="1")
    {
        try
        {
            Response.ContentType = "html/text";
            Response.AddHeader("Content-Disposition", "attachment; filename=file.txt");
            Response.Write("content of the file");
            Response.Flush();
            Response.Close();
            Response.End();
        }
        catch (Exception)
        {
            //An error occurred
            Response.Redirect("Error.aspx");
        }
    }
}

由于它只是一个链接,如果找不到文件,浏览器将显示“未找到”。如果服务器端出现错误,则重定向到错误页面。如果您想要更详细的解决方案,我建议您使用 XMLHttpRequest。

【讨论】:

  • 谢谢,很好的解决方案。我的按钮实际上是一个 asp.net 按钮,老实说,我想删除之前显示的一些错误消息。我只是不想添加令人困惑的细节。我将再次编辑帖子并添加它。
  • 对于 asp 按钮,有一个名为 OnClientClick 的属性。你可以把你的javascript放在那里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
相关资源
最近更新 更多