Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

c# - Download feature not working within update panel in asp.net

I have a Web User Control containing a FormView. The formview shows details of job seeker. I have provided a button for "Download Resume" link, so that admin/HR can download the resume. I have placed this control in an aspx page that contains the UpdatePanel. Everything works fine except Download Link.

I have given a Command on donwload link button and a function is associated with the command to start download.

Below is the code i have implemented -

//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");
    }
}

The code works perfectly if update panel is removed but generates script errors if update panel is used.

Any suggestions....?

Thanks for sharing your time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To initiate a full page postback, you add a postback trigger to your update panel:

<asp:UpdatePanel runat="server">
    <Triggers>
        <asp:PostBackTrigger ControlID="YourControlID" />
    </Triggers>
    <ContentTemplate>
        .....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...