文件下载后更新页面

时间:2010-10-26 15:34:22

标签: c# asp.net ihttphandler

我在前几天堆栈溢出的一些精彩帮助之后整理了一个下载脚本。但是我现在发现在下载文件之后我需要重新加载页面以摆脱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; } } 

感谢您提供的任何帮助!

4 个答案:

答案 0 :(得分:28)

当您发回文件供用户下载时, 是HTTP请求。换句话说,您可以 进行回复,刷新浏览器页面,您可以发送文件供用户下载。如果没有特殊的技巧,你不可能做到这两点。

这就是为什么大多数网站在您下载文件时,首先会带您进入一个新页面,上面写着“您的下载即将开始”,然后随后将您“重定向”到使用元刷新或javascript下载文件。

例如,当你去这里下载.NET 4运行时:

http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=0a391abd-25c1-4fc0-919f-b21f31ab88b7&displaylang=en&pf=true

它呈现页面,然后使用以下元刷新标记实际为用户提供要下载的文件:

<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" />

您可能需要在应用中执行类似的操作。但是,如果您真的对 之后完全下载文件感兴趣,那么您运气不好,因为没有事件可以将其传达给浏览器。唯一的方法是使用AJAX upload gmail在上传附件时使用。

答案 1 :(得分:6)

就我而言,我使用的是MVC,我只是希望在选择下载按钮后几秒刷新页面以显示新的下载次数。我从控制器返回文件。

为此,我只是通过向调用以下脚本的下载按钮添加onclick事件(也在视图中)来更改视图:

setTimeout(function () {
        window.location.reload(1);
    }, 5000);

这符合我的目的......希望它可以帮助别人。

答案 2 :(得分:1)

如果需要的话,这很容易被黑客入侵。

步骤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);

答案 3 :(得分:0)

一种更简单的方法是只执行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答案类似,但没有“不同步”超时的缺点。