文件未打开

时间:2011-03-31 07:05:43

标签: asp.net upload

我有一个网格,其中文件名显示为链接按钮。但是当我点击链接按钮时,文件无法打开。请参阅cs文件代码方法打开文件。

protected void OpenFile(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        string fileName = btn.Attributes["FileName"].ToString();

        string path = Server.MapPath(".") + "\\Files\\" + fileName;
        if (File.Exists(path))
        {
            Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
            string type = "Application/word";
            if (type != "")
                Response.ContentType = type;
            Response.WriteFile(path);
            Response.End();
        }
        else
        {
        }

        //System.Diagnostics.Process.Start(@path);
        //Response.Write(path);   
    }

以下是Gridview的代码:

<asp:GridView ID="gdvMainList" runat="server" CssClass="Grid" 
                                                AutoGenerateColumns="False" DataSourceID="dtsFilesUploaded" 
                                                AllowPaging="True" DataKeyNames="Id, FileName" 
                                                onrowdatabound="gdvMainList_RowDataBound">
                                                <Columns>
                                                    <ucc:CommandFieldControl HeaderText="Actions" ShowDeleteButton="true" ButtonType="Image"
                                                        DeleteImageUrl="~/App_Themes/Default/images/delete.png" ShowEditButton="true"
                                                        EditImageUrl="~/App_Themes/Default/images/edit.png" ShowCancelButton="true" CancelImageUrl="~/App_Themes/Default/images/cancel.png"
                                                        UpdateImageUrl="~/App_Themes/Default/images/update.png" DeleteConfirmationText="Are you sure you want to delete?">
                                                        <ItemStyle HorizontalAlign="Center" Width="60px" />
                                                        <HeaderStyle HorizontalAlign="Center" />
                                                    </ucc:CommandFieldControl>
                                                    <asp:TemplateField HeaderText="File Name">
                                                        <ItemTemplate>
                                                            <asp:LinkButton ID="lblFileName" runat="server" Text='<%# Bind("FileName") %>' OnClick="OpenFile" CausesValidation="false"></asp:LinkButton>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Uploaded On">
                                                        <ItemTemplate>
                                                            <asp:Label ID="lblCreatedDate" runat="server" Text='<%# Bind("CreatedDateTime","{0:d}") %>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Category">
                                                        <ItemTemplate>
                                                            <asp:Label ID="lblglCategoryId" runat="server" Text='<%# Bind("glCategoryId") %>'>' ></asp:Label>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <asp:DropDownList ID="ddlglCategoryId" runat="server" CssClass="textEntry2" DataSourceID="dtsglCategoryId"
                                                                DataTextField="LookupItem" DataValueField="Id" AppendDataBoundItems="true">
                                                            </asp:DropDownList>
                                                        </EditItemTemplate>
                                                    </asp:TemplateField>

                                                    <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="true" />
                                                </Columns>
                                                <EmptyDataTemplate>
                                                    <div class="divEmptyListingGrid">
                                                        --- No Files Exists ---
                                                    </div>
                                                </EmptyDataTemplate>
                                            </asp:GridView>

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

更新面板内无法使用响应上下文。

可在此处找到替代方案:http://forums.asp.net/p/1030846/1414305.aspx