下载function在asp.net的更新面板中不起作用

我有一个包含FormView的Web用户控件。 formview显示求职者的详细信息。 我已经为“下载简历”链接提供了一个按钮,以便admin / HR可以下载简历。 我已将此控件放在包含UpdatePanel的aspx页面中。 除下载链接外,一切正常。

我在donwload链接按钮上给出了一个命令,并且一个函数与该命令相关联以开始下载。

以下是我实施的代码 –

 //Command on 'Download' link button within FormView protected void lnkDownload_Command(object sender, CommandEventArgs e) { if (e.CommandName.Equals("Download")) { StartDownload(e.CommandArgument.ToString()); } } //My routine to download document //sFileInfo contains filepath$==$mimetype protected void StartDownload(string sFileInfo) { string[] d = sFileInfo.ToString().Split((new string[] { "$==$" }), StringSplitOptions.None); string filename = d[0]; string docType = d[1]; System.IO.FileInfo file = new System.IO.FileInfo(d[0]); if (file.Exists) { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + d[0]); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = d[1]; Response.WriteFile(file.FullName); Response.End(); } else { Server.Transfer("~/Mesgbox.aspx?cat=2"); } } 

如果删除更新面板,则代码可以正常工作,但如果使用更新面板,则会生成脚本错误。

有什么建议….?

谢谢你分享你的时间。

要启动整页回发,请在更新面板中添加回发触发器:

      ..... 

您无法在UpdatePanel部分回发中返回附件,因为ScriptManager使用结果更新DIV(而不是整个响应)。 您尝试做的最简单的修复方法是将下载按钮设置为回发控件。 这将导致该按钮启动完整回发。 以下代码包含在您的Page_Load中

 ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(this.lnkDownload); 

您仍然可以从更新面板中触发下载文档。

我有一个更新面板,里面有3个嵌套的中继器。 在最内部的转发器中,我使用LinkBut​​tons构建了一系列下载链接,每个链接都包含一个通过webservice获取文档并发送它的命令。

每个转发器都有一个OnItemDataBound方法。 在最后一个中继器中,我有以下内容

  protected void LinkDocRepeaterOnItemDataBound(object sender, RepeaterItemEventArgs e) { if(!(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)) { return; } LinkButton linkButton = (LinkButton)e.Item.FindControlRecursive("LinkId"); var scriptManager = ScriptManager.GetCurrent(this.Page); if (scriptManager != null) { scriptManager.RegisterPostBackControl(linkButton); } } 

每个Linkbutton现在都下载一个文档。

我的情况:

我有一个从sql数据加载的长期运行excel文件,我希望更新进度面板在创建文件时显示spinner gif,然后从更新面板中下载文件。 它比我想象的要棘手。

这个链接在搜索时很高,在尝试避免之后,结果发现iframe对我很有用。

iframe异步下载

这是最终工作..(这个确切的代码尚未经过测试)

MyPage.aspx …(内部更新面板,没有触发器)

   

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(); } } 

它似乎像任何其他异步回发一样按预期工作。

您无法在更新面板中使用Response对象。

‘runat =“server”OnClick =“DownloadFile”>’runat =“server”OnClick =“DeleteFile”/> <%----%>

protected void UploadFile(object sender,EventArgs e){if(FileUpload1.HasFile){string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName); string extension =“。” + FileName.Split(’。’)[1] .ToString(); string FileName_Guid = Convert.ToString(Guid.NewGuid())+ extension; FileUpload1.PostedFile.SaveAs(@“C:\ Uploads \”+ FileName_Guid); string Platform_Config_ID = PlatformConfigID.Value; DataTable dt = new DataTable(); dt = DAL.Upload_File(FileName_Guid,FileName,Platform_Config_ID);

  gv_Files.DataSource = dt; gv_Files.DataBind(); ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); scriptManager.RegisterPostBackControl(gv_Files); } } protected void DownloadFile(object sender, EventArgs e) { try { LinkButton lnkDownload = (LinkButton)sender; GridViewRow row = (GridViewRow)lnkDownload.NamingContainer; LinkButton download = row.FindControl("lnkDownload") as LinkButton; ScriptManager.GetCurrent(this).RegisterPostBackControl(download); string FileName = (sender as LinkButton).CommandArgument.Split(';')[0].ToString(); string OriginalFileName = (sender as LinkButton).CommandArgument.Split(';')[1].ToString(); string FilePath = @"C:\Uploads\" + FileName.ToString(); FileInfo file = new FileInfo(FilePath); if (file.Exists) { Response.ContentType = ContentType; Response.AppendHeader("Content-Disposition", "attachment; filename=" + OriginalFileName); Response.Headers.Set("Cache-Control", "private, max-age=0"); Response.WriteFile(FilePath); Response.End(); } } catch (Exception ex) { // do nothing } } protected void DeleteFile(object sender, EventArgs e) { string FileName_Guid = (sender as LinkButton).CommandArgument.Split(';')[0].ToString(); string File_ID = (sender as LinkButton).CommandArgument.Split(';')[1].ToString(); string Filename = (sender as LinkButton).CommandArgument.Split(';')[2].ToString(); string Platform_Config_ID = (sender as LinkButton).CommandArgument.Split(';')[3].ToString(); string FilePath = @"C:\Uploads\" + FileName_Guid; File.Delete(FilePath); DataTable dt = new DataTable(); dt = DAL.Delete_File(File_ID, Filename, Platform_Config_ID); gv_Files.DataSource = dt; gv_Files.DataBind(); } 

请尝试以下步骤:

  1.    
  2. (代码背后) UploadFile()

     ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); scriptManager.RegisterPostBackControl(gv_Files); 
  3. DownloadFile()

     DownloadFile() --> LinkButton download = row.FindControl("lnkDownload") as LinkButton; ScriptManager.GetCurrent(this).RegisterPostBackControl(download);