[英]Update page after file download
前幾天我在堆棧溢出的一些精彩幫助之后整理了一個下載腳本。 但是我現在發現在下載文件之后我需要重新加載頁面以擺脫aspx頁面上的進度模板。 在我添加下載代碼之前,刪除模板的代碼工作正常。
刪除進度模板的代碼: upFinanceMasterScreen.Update();
我已經嘗試在重定向到IHttpHandler
之前和之后調用它
Response.Redirect("Download.ashx?ReportName=" + "RequestingTPNLeagueTable.pdf");
public class Download : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
StringBuilder sbSavePath = new StringBuilder();
sbSavePath.Append(DateTime.Now.Day);
sbSavePath.Append("-");
sbSavePath.Append(DateTime.Now.Month);
sbSavePath.Append("-");
sbSavePath.Append(DateTime.Now.Year);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpResponse objResponce = context.Response;
String test = HttpContext.Current.Request.QueryString["ReportName"];
HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + test);
objResponce.WriteFile(context.Server.MapPath(@"Reports\" + sbSavePath + @"\" + test));
}
public bool IsReusable { get { return true; } }
感謝您的任何幫助,您可以提供!
當您發回文件供用戶下載時, 即 HTTP請求。 換句話說,您可以進行回復以刷新瀏覽器頁面,也可以發送文件供用戶下載。 如果沒有特殊的技巧,你不可能做到這兩點。
這就是大多數網站下載文件時的原因,它首先會將您帶到一個新頁面,上面寫着“您的下載即將開始”,然后隨后使用元刷新或javascript將您“重定向”到文件中進行下載。
例如,當您到這里下載.NET 4運行時:
它呈現頁面,然后使用以下元刷新標記實際為用戶提供要下載的文件:
<META HTTP-EQUIV="refresh" content=".1; URL=http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe" />
你可能需要在你的應用程序中做類似的事情。 但是,如果您真的對在文件完全下載后做某事感興趣,那么您運氣不好,因為沒有事件可以將其傳達給瀏覽器。 唯一的方法是在上傳附件時像gmail一樣使用AJAX上傳 。
在我的情況下,我正在使用MVC,我只是希望在選擇下載按鈕后幾秒刷新頁面以顯示新的下載計數。 我從控制器返回文件。
為此,我只需通過向調用以下腳本(也在視圖中)的下載按鈕添加onclick事件來更改視圖:
setTimeout(function () {
window.location.reload(1);
}, 5000);
它符合我的目的......希望它能幫助別人。
如果需要,這很容易被破解。
第1步:向.aspx頁面添加隱藏按鈕:
<asp:Button ID="btnExportUploaded" runat="server" Text="Button" style="visibility:hidden" OnClick="btnExportUploaded_Click" CssClass="btnExportUploaded" />
步驟2:執行默認的回發操作,最后使用jquery調用注冊啟動腳本,這將觸發隱藏按鈕單擊並導致文件下載:
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), "modalstuff", "$('.btnExportUploaded').click();", true);
一種更簡單的方法是在PostBack
事件中執行任何PostBack
,並使用附加參數注冊重載腳本以指示下載。 就像是:
C#代碼:
protected void SaveDownloadCount(int downloadId)
{
// Run in a PostBack event.
// 1) Register download count, refresh page, etc.
// 2) Register a script to reload the page with an additional parameter to indicate the download.
Page.ClientScript.RegisterStartupScript(GetType(), "download",
"$(document).ready(function(){window.location.href = window.location.pathname + window.location.search ? '&' : '?' + 'printId={0}';});".Replace("{0}", downloadId.ToString()), true);
}
然后,在PageLoad
我們需要檢查下載參數並提供文件:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int printId;
if (Request.QueryString["printId"] != null && int.TryParse(Request.QueryString["printId"], out printId))
{
// Check if the argument is valid and serve the file.
}
else
{
// Regular initialization
}
}
}
這對於@puddleglum回答來說是simalar,但沒有“不同步”超時的缺點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.