Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My situation:</p> <p>I had a long running excel file loaded from sql data and I wanted update progress panel to display the spinner gif while the file was created and then download the file from within the update panel. It was trickier than I thought.</p> <p>This link came up high on searches and after trying to avoid it, it turned out that an iframe was useful to me.</p> <p><a href="http://encosia.com/ajax-file-downloads-and-iframes/" rel="nofollow">iframe async download</a></p> <p>Here is what ended up working.. (this exact code has not been tested)</p> <p>MyPage.aspx...(inside update panel, no triggers)</p> <pre><code>&lt;asp:Button runat="server" ID="btnExcelExport" Text="Export to Excel" OnClick="btnExcelExport_Click" /&gt; &lt;iframe runat="server" id="ifmExcel" width="0" height="0" marginheight="0" marginwidth="0" frameborder="0" /&gt; </code></pre> <p>MyPage.aspx.cs</p> <pre><code> protected void btnExcelExport_Click(object sender, EventArgs e) { //long running process here, taking advantage of the update progress panel var bytes = GetExcelFile(); //generate a key to pass to the download page to access the file bytes var cacheKey = Guid.NewGuid().ToString("N");//N means no hyphens //placing the result in cache for a few seconds so the download page can grab it Context.Cache.Insert(key: cacheKey, value: bytes, dependencies: null, absoluteExpiration: DateTime.Now.AddSeconds(30), slidingExpiration: System.Web.Caching.Cache.NoSlidingExpiration); ifmExcel.Attributes.Add("src", String.Format("MyDownloadPage.aspx?cacheKey={0}", cacheKey)); } </code></pre> <p>MyDownloadPage.aspx.cs...</p> <pre><code> protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { var bytes = Context.Cache.Get(Request.QueryString.Get("cacheKey")) as byte[]; Response.Clear(); Response.AddHeader( "content-disposition", string.Format("attachment; filename={0}.xlsx", "Invoice")); Response.ContentType = "application/xlsx"; Response.BinaryWrite(bytes); Response.End(); } } </code></pre> <p>It seems to work as expected like any other async post back would.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload