Reputation: 124
Even though the file is downloading, I am not able to get pop up message or even label lblDownload
text not changing inside 'if condition' if it goes for 'else' condition pop up message coming.
protected void btn1_Click(object sender, EventArgs e)
{
string filePath = txt1.Text;
if (filePath != "")
{
lblDownloadS1.Text = "File downloaded successfully please check in downloads";
Response.Write("<script>alert('File downloaded succesfully')</script>");
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
else
{
Response.Write("<script>alert(' Specified file not exist')</script>");
}
}
Upvotes: 0
Views: 3536
Reputation: 295
string filePath = txt1.Text;
if (filePath != "")
{
lblDownloadS1.Text = "File downloaded successfully please check in downloads";
Response.Write("<script>alert('File downloaded succesfully')</script>");
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
// try this --- Response.Write("<script>window.open('lblDownloadS1.Text'-blank');</script>");
Response.End();
}
else
{
Response.Write("<script>alert(' Specified file not exist')</script>");
}
Upvotes: 0
Reputation: 25351
You cannot. The code for the file download must end with Response.End()
like you did, and cannot write to the response after that. Similarly, although you changed lblDownloadS1.Text
before Response.End()
, it still won't show up because the download effectively cancelled it. In other words, you cannot do anything on a download page other than setting the headers. The body of the page must be the downloaded file and nothing else.
The only way to do it is to make the page that downloads the file a popup. So you cannot use ASP.Net button click event. Instead, make it an HTML button (or link) that calls a JavaScript function. The JavaScript function opens the download page as a popup, and then it display the alert
. However, this will display the alert
shorty after the download, and there is no way to wait for the download to finish. For that reason, it is better to use a <div>
popup instead of alert
. The JavaScript function can also change the text of label and anything else you want to do on the page.
Upvotes: 0
Reputation: 81
Currently you are popup before Downloading start
Put Response.write (popup script) Before Response.end()
Try it
Upvotes: 1