Show a message after sending a file to download

試著忘記壹切 提交于 2019-11-29 12:59:39

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!