Redirecting in EP user control to download a file

If you have a webpage to generate files such as word doc or pdf and you need to invoke them in a button click event of an user control used in EP and allow the user to download the file through the browser. generally you would think of calling response.redirect function. This function redirects to the download page and since it sets the mime type as the respective file, it prompts the user to save the file to the local machine or open it using the right application. Since AX usercontrols inherently uses AJAX postbacks, the postback progressbar will continue to display "Loading Information" even after the file is downloaded when response.redirect is used. This is because response.redirect supposed to replace the current page displayed in the browser, but in this case it actually streams file and does not replace the currently rendered page in the browser. For such scenarios, instead of using response.redirect, you could execute a client side javascript to push the generated file in a new browser window and avoid the above issue.

 

protected void Button1_Click(object sender, EventArgs e)

{

AxUrlMenuItem menuItem = new AxUrlMenuItem("EPDocuGet");

DataSetViewRow row = dsEPCustInvoiceJournalInfo.GetDataSet().DataSetViews[0].GetCurrent();

AxTableContext context = AxTableContext.Create(row.GetDefaultTableDataKey(row.DataSetView.Metadata.RootDataSource));

menuItem.MenuItemContext = context;

//Response.Redirect(menuItem.Url.ToString());

string downloadURL = "window.open('" + menuItem.Url.ToString() + "', '_blank', '' );";

ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", downloadURL, true);

}