Моя ситуация:
У меня был долго загруженный файл Excel, загруженный из данных sql, и я хотел, чтобы панель прогресса обновления отображала GIF счетчика при его создании, а затем загружала файл из панели обновления.Это было сложнее, чем я думал.
Эта ссылка показала много результатов при поиске, и после попытки избежать этого оказалось, что iframe был полезен для меня.
iframe async download
Вот что в итоге заработало .. (этот точный код не был проверен)
MyPage.aspx ... (внутри панели обновления, без триггеров)
<asp:Button runat="server" ID="btnExcelExport" Text="Export to Excel" OnClick="btnExcelExport_Click" />
<iframe runat="server" id="ifmExcel" width="0" height="0" marginheight="0" marginwidth="0"
frameborder="0" />
MyPage.aspx.cs
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));
}
MyDownloadPage.aspx.cs ...
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();
}
}
Кажется, что все работает как ожидалось, как и любая другая асинхронная запись назад.