Doc文件无法从asp.net中的UserControl下载

本文关键字:net 中的 UserControl 下载 asp 文件 Doc | 更新日期: 2023-09-27 18:19:13

我有一个包含候选数据的网格的用户控件。有一个列候选名称与模板字段链接按钮。我附加了一个行命令事件,我正在下载一个word文件。我下载的文件代码下载我的文件文件从简单的网页,但这段代码是不工作的用户控制。有人能帮我解决这个问题吗?它给出的错误响应是not available

  <asp:GridView ID="grdCandidate" runat="server" AutoGenerateColumns="false" 
       OnRowDataBound="grdCandidate_RowDataBound" 
       onrowcommand="grdCandidate_RowCommand">
          <Columns>
             <asp:BoundField DataField="Candidate ID" HeaderText="Candidate ID" />
                 <asp:TemplateField>
                      <HeaderTemplate>
                            Candidate Name
                      </HeaderTemplate>
                  <ItemTemplate>
                            <asp:LinkButton ID="lnkResume" CommandName="Download" CommandArgument='<%#Eval("Candidate ID") %>'
                                runat="server" Text='<%#Eval("Candidate Name") %>' ToolTip='<%# "Download Resume - " + Eval("Candidate Name") %>'></asp:LinkButton>
                  </ItemTemplate>
                </asp:TemplateField>
             </Columns>
 </asp:GridView>
protected void grdCandidate_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName == "Download")
        {
            byte[] Attachment = null;
            string Extension = string.Empty;
            string Resume = "Resume";
            ClsCandidateManager objCandidateManager = new ClsCandidateManager();
            ClsSecureManager objSecureManager = new ClsSecureManager();
            Attachment = objCandidateManager.GetCandidateAttachment(Convert.ToInt32(e.CommandArgument), out Extension);
            if (Attachment != null && Attachment.Length > 0)
            {
                try
                {
                    Response.Clear();
                    Response.Buffer = true;
                    Response.Charset = "";
                    if (Extension == ".pdf")
                    {
                        Response.ContentType = "application/pdf";
                    }
                    else
                    {
                        Response.ContentType = "application/vsd-msword";
                    }
                    Response.AddHeader("content-disposition", "attachment;filename=" + Resume + Extension);
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.BinaryWrite(Attachment);                        
                    Response.Flush();
                    Response.End();
                }
                catch (Exception ex)
                {
                    string str = ex.Message + ex.InnerException;
                }
            }
            else
            {
                //ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script type='text/javascript'>alert('Resume is not Uploaded !');</script>");
            }
        }
    }
    catch (Exception ex)
    {
        string str = ex.Message + ex.InnerException;
    }

Doc文件无法从asp.net中的UserControl下载

使用如下所示的UpdatePanel,

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                            <asp:LinkButton ID="lnkDownload" runat="server" Text="View" OnClick="lnkDownload_Click"
                                CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
                        </ContentTemplate>
                        <Triggers>
                            <asp:PostBackTrigger ControlID="lnkDownload" />
                        </Triggers>
                    </asp:UpdatePanel>