user4477059
user4477059

Reputation: 21

Code which is to be executed after file download doesn't work in Asp.net

I have a ASP.NET application which allows the user to download file automatically on the client side but after the file downloads my code which is to be executed after file download doesn't work, my page stops working and remains halt until i refresh the page. My code is

            cloudBarcodeService.generateFile_Barcode(barcodeList).WriteTo(writer);
            writer.Flush();
            Response.Clear();
            byte[] byteArray = stream.ToArray();
            Response.AppendHeader("Content-Disposition", "filename=barcode.xml");
            Response.AppendHeader("Content-Length", byteArray.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.BinaryWrite(byteArray);
            writer.Close();

            //following text doesn't update
            lblSucess.text="Successfully Downloaded";

Upvotes: 2

Views: 1666

Answers (2)

user4477059
user4477059

Reputation: 21

Thanks everyone for your help my problem resolved by using below code in aspx page

<asp:PlaceHolder ID="PlaceIframe" runat="server" Visible="false">
<iframe src="<%=DownloadUrl %>" style="width:1px; height:1px; display:none;"></iframe>
</asp:PlaceHolder>

by assigning url to DownloadUrl from code behind.

Upvotes: 0

Kjartan
Kjartan

Reputation: 19111

When you send a file to the client this way, you are specifying the header, content type, and everything to download the data as a file, and not something to be displayed on the page. When the download is done, it is done, and the connection is closed - you can't do anything else with it before the client initiates some new action.

What you might be able to do is initiate a redirect (examples in a different SO question) before you actually start the download - that might work, and let you redirect back to your original page a few seconds after the download has been started. There, you could accept a parameter or something to indicate that a "successfully downloaded" message should be displayed.

On the other hand, are you sure you even want to do this? Once a download starts, the client (which can presumably be one of a large number of different browsers) will generally let the user know how the download goes, and when it completes. On the server side, you probably don't even know how it ends - all the server knows is that it has been sent.

Upvotes: 1

Related Questions