Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

c# - Show a message after sending a file to download

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();

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

In my code behind I have

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");
        }
    }
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...