radgrid 中的下载功能问题

Downloading functionality issue in radgrid

我有一个棘手的问题,希望我能解释清楚。

这是我的问题: 我有一个 radgrid FormTemplate 用于 Adding/Editing 行,我在其中使用了 RadAsyncUpload 控件上传文件。该文件在插入和更新时已成功上传。此外,我在我的 radgrid 视图中配置了一个 link 按钮,可以在需要时下载文件。

现在的问题是:上传文件后立即尝试从 radgrid 视图下载时,无法下载。相反,我需要先重新加载页面,然后才能下载文件。可能是什么问题呢?我还使用了 grid.rebind() 方法,但这不起作用。以下是我的代码的简要区域:

<Columns>
                        <telerik:GridButtonColumn Text="Download" HeaderText="Responsible" ItemStyle-ForeColor="Blue" CommandName="DownloadResponsibleAttachment" ColumnGroupName="Attachments"
                            ConfirmTextFields="ResponsibleAttachment" ConfirmTextFormatString="Would you like to download attachment?" HeaderStyle-Width="30px"
                            ConfirmDialogType="RadWindow" Reorderable="false" UniqueName="DownloadResponsibleAttachment" ConfirmTitle="File Download">
                        </telerik:GridButtonColumn>
                    </Columns>
                    <EditFormSettings EditFormType="Template">
                        <FormTemplate>
                            <p class="lbl" style="font: 10px verdana, arial; color: #166c92; font-weight: bold;">Responsible Attachment</p>
                            <telerik:RadAsyncUpload ID="attResponsible" runat="server" InitialFileInputsCount="1" MaxFileInputsCount="1" MaxFileSize="1048576"
                                 TargetFolder="~/Attachments" TemporaryFolder="~/Attachments" TemporaryFileExpiration="600000" ControlObjectsVisibility="None"
                                 OverwriteExistingFiles="true" ToolTip="You can only upload one file with maximum 1 MB." OnFileUploaded="attResponsible_FileUploaded" >
                            </telerik:RadAsyncUpload>

                            <div style="float:right; padding-top:20px;">
                            <asp:Button ID="btnUpdate" CssClass="btn" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>&nbsp;
                            <asp:Button ID="btnCancel" CssClass="btn" Text="Cancel" runat="server" CausesValidation="False"
                                CommandName="Cancel"></asp:Button>
                        </FormTemplate>
                    </EditFormSettings>

如果要从 AJAX-enabled control(位于 RadAjaxPanel or ajaxified by RadAjaxManager 中)调用文件下载,则不能使用正常的 response.WriteFile function.

Response.WriteFile("~/filename");

因为 XmlHttpRequest 无法处理。

编辑:

Please check this link for alternative

ASPX代码:

<div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadWindowManager ID="RadWindowManager1" runat="server">
        </telerik:RadWindowManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
            <ContentTemplate>
                <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemCommand="RadGrid1_ItemCommand"
                    OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
                    <MasterTableView Width="950" AutoGenerateColumns="false" DataKeyNames="ID" GridLines="None"
                        TableLayout="Auto">
                        <Columns>
                            <telerik:GridBoundColumn DataField="ID" HeaderText="Emp ID" ReadOnly="true" HeaderStyle-HorizontalAlign="Left"
                                ItemStyle-HorizontalAlign="Left" UniqueName="ID" FilterControlWidth="30px" AutoPostBackOnFilter="true"
                                CurrentFilterFunction="Contains" />
                            <telerik:GridButtonColumn DataTextField="Name" ItemStyle-ForeColor="Blue" CommandName="Generate"
                                ConfirmTextFields="Name" ConfirmTextFormatString="Would you like to  {0} ACH file ?"
                                ConfirmDialogType="RadWindow" Reorderable="false" UniqueName="ButtonName" ConfirmTitle="ACH File">
                            </telerik:GridButtonColumn>
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>

隐藏代码:

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data = new[] {
             new { ID = 1, Name ="Name_1"},
               new { ID = 2, Name = "Name_2"},
               new { ID = 3, Name = "Name_3"},
               new { ID = 4, Name = "Name_4"},
               new { ID = 5, Name = "Name_5"}
        };

        RadGrid1.DataSource = data;

    }


    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            ScriptManager.GetCurrent(Page).RegisterPostBackControl(item["ButtonName"].Controls[0]);

        }
    }


    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Generate")
        {
            Response.ContentType = "text/plain";
            Response.AppendHeader("Content-Disposition", "attachment;filename= errorLog.txt");
            Response.AddHeader("content-length", "0");
            Response.Flush();
            Response.End();
        }
    }

我应用了一个解决方案来在记录成功时重新加载页面inserted/updated,如下所示:

protected void grid_ItemInserted(object source, GridInsertedEventArgs e)
    {
        if (e.Exception == null)
        {
            if (Session["FileUploaded"] != null)
                Response.Redirect(Request.RawUrl);
        }
    }

仅作记录之用,我想post回答我遇到的问题。我找到了解决此问题的方法。 RadAjaxManager 似乎有问题,所以我用 RadAjaxPanel 替换了它,这对我来说非常有用。