发送文件下载后显示消息 [英] Show a message after sending a file to download

查看:76
本文介绍了发送文件下载后显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to update something on my aspx page, after I send a file to be downloaded. (Some error messages shown before.). I believe it's not possible, but would you provide me a solution?

以下是发送文件以下载的代码:

Here is the code to send the file to download:

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

编辑以澄清:我也相信没有逻辑上的解决方案.但是,可能有一个我不知道的Javascript技巧.

Editing to clarify: I also believe that there is no logical solution. However, there could be a Javascript trick which I am not aware of.

推荐答案

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

That's the most simple way I can imagine.

<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.

Since it's just a link, if the file is not found the browser will display "not found". If there's an error at server side then redirect to error page. If you want a more elaborated solution I'd suggest using XMLHttpRequest.

这篇关于发送文件下载后显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆