I have a page, behaviorAnalysis.aspx, that is calling a javascript that does two things: 1) Display a modal dialog with a please wait message; and 2) creating an iFrame and calling a second page, behaviorAnalysisDownload.aspx via jQuery:
function isMultiPageExport(exportMedia) {
waitingDialog.show("Building File<br/>...this could take a minute", { dialogSize: "sm", progressType: "warning" });
var downloadFrame = document.createElement("IFRAME");
if (downloadFrame != null) {
downloadFrame.setAttribute("src", 'behaviorExport.aspx?exportType=html&exportMedia=' + exportMedia);
downloadFrame.style.width = "0px";
downloadFrame.style.height = "0px";
document.body.appendChild(downloadFrame);
}
}
The second page is downloading an Excel file using the following code snippet:
//*****************************************
//* Workbook Download & Cleanup
//*****************************************
MemoryStream stream = new MemoryStream();
wb.Write(stream);
stream.Dispose();
var xlsBytes = stream.ToArray();
string filename = "Behavior Stats YTD.xlsx";
MemoryStream newStream = new MemoryStream(xlsBytes);
if (Page.PreviousPage != null)
{
HiddenField exp = (HiddenField)Page.PreviousPage.FindControl("hidDownloadStatus");
exp.Value = "Complete";
}
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
HttpContext.Current.Response.BinaryWrite(xlsBytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
As you can see, I was hoping to update a hidden field on the calling page before pushing the download through; however, PreviousPage is null. Is there another approach I can use, to update the hidden field value from the calling page, behaviorAnalysis.aspx?
I would first recommend this jQuery library, which does exactly what you want with the background download and modal, but it's been tested cross-browser and has a lot of neato features already available: http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
If you don't like that, you can take a similar approach to what that plugin does. Instead of trying to set the value of a HiddenField on the parent, just add a cookie:
Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" });
After your first page appends the iFrame, just use setInterval() to check to see if that cookie exists with something like:
if (document.cookie.indexOf('fileDownload') > -1) {
document.cookie = 'fileDownload=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;' // remove cookie
// it was a success, so do something here
}
Of course, you'll want to put some sort of a timeout or logic to handle errors, but that should cover the basics of it.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments