DetailsView中的FileUpload:HasFile始终为false

时间:2011-04-21 14:56:32

标签: c# .net asp.net visual-studio-2010

首先,我没有使用UpdatePanel - 看起来这是一个常见的问题,但在我搜索这个问题之前我甚至不知道是什么。

我有一个DetailsView尝试上传文件并将文件名插入数据库。问题是,HasFile总是出现错误!知道我做错了吗?

C#:

public partial class DocManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(System.Convert.ToBoolean(Session["Admin"])))
            Response.Redirect("Index.aspx");
    }

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
    FileUpload fu1 = (FileUpload) DetailsView1.FindControl("FileUpload1");
    if (fu1 == null)
    {
        e.Cancel = true;
        StatusLabel.Text = "Could not find file upload";
    }
    if (fu1.HasFile)
    {
        try
        {
            string filename = Path.GetFileName(fu1.FileName);
            fu1.SaveAs(Server.MapPath("~/Docs/") + filename);
            StatusLabel.Text = "Upload status: File uploaded!";
            e.Values["FileName"] = filename;
        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }

    }
    else
    {
        e.Cancel = true;
        StatusLabel.Text = "No file uploaded";
        return;
    }

    DropDownList dd1 = (DropDownList)DetailsView1.FindControl("DropDownList2");
    DropDownList dd2 = (DropDownList)DetailsView1.FindControl("DropDownList4");
    e.Values["Type"] = dd1.SelectedValue;
    e.Values["MeetingID"] = dd2.SelectedValue;

}

protected void DetailsView1_ItemEditing(object sender, DetailsViewInsertEventArgs e)
{
    FileUpload fu1 = (FileUpload)DetailsView1.FindControl("FileUpload2");
    if (fu1 == null)
        e.Cancel = true;
    if (fu1.HasFile) {
        try
        {
            string filename = Path.GetFileName(fu1.FileName);
            fu1.SaveAs(Server.MapPath("~/Docs/") + filename);
            StatusLabel.Text = "Upload status: File uploaded!";
            e.Values["FileName"] = filename;
        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
    else
        e.Cancel = true;

    DropDownList dd1 = (DropDownList)DetailsView1.FindControl("DropDownList1");
    DropDownList dd2 = (DropDownList)DetailsView1.FindControl("DropDownList3");
    e.Values["Type"] = dd1.SelectedValue;
    e.Values["MeetingID"] = dd2.SelectedValue;

}

}

PageLoad中唯一的一项是检查以确保用户正确登录,之前没有其他任何东西应该运行。

表单本身:

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
    AutoGenerateRows="False" DataSourceID="Docs" DefaultMode="Insert"
     OnItemInserting="DetailsView1_ItemInserting"
     OnItemEditing="DetailsView1_ItemEditing"
    >
    <Fields>
        <asp:BoundField DataField="Documents.Title" HeaderText="Title" 
            SortExpression="Documents.Title" />
        <asp:TemplateField HeaderText="Type" SortExpression="Type">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
                    <asp:ListItem Value="Handout">Handout</asp:ListItem>
                    <asp:ListItem Value="Minutes">Minutes</asp:ListItem>
                    <asp:ListItem Value="Agenda">Agenda</asp:ListItem>
                    <asp:ListItem Value="Other">Other</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:DropDownList ID="DropDownList2" runat="server">
                    <asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
                    <asp:ListItem Value="Handout">Handout</asp:ListItem>
                    <asp:ListItem Value="Minutes">Minutes</asp:ListItem>
                    <asp:ListItem Value="Agenda">Agenda</asp:ListItem>
                    <asp:ListItem Value="Other">Other</asp:ListItem>
                </asp:DropDownList>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Type") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="MeetingID" SortExpression="MeetingID">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="Meetings" 
                    DataTextField="Title" DataValueField="MeetingID">
                </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="Meetings" 
                    DataTextField="Title" DataValueField="MeetingID">
                </asp:DropDownList>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("MeetingID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="FileName" SortExpression="FileName">
            <EditItemTemplate>
                <asp:FileUpload ID="FileUpload2" runat="server" />
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("FileName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
    </Fields>
</asp:DetailsView>

ETA:整个表格:

<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" Runat="Server">
<h1>Document Manager</h1>

<h2>Existing Documents</h2>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
    AutoGenerateColumns="False" DataSourceID="Docs"
    OnRowDeleting="GridView1_RowDeleting"
    DataKeyNames="DocID" BackColor="White" BorderColor="#E7E7FF" 
    BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
    <AlternatingRowStyle BackColor="#F7F7F7" />
    <Columns>
        <asp:BoundField DataField="Documents.Title" HeaderText="Title" 
            SortExpression="Documents.Title" />
        <asp:BoundField DataField="FileName" HeaderText="FileName" 
            SortExpression="FileName" />
        <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
        <asp:BoundField DataField="Meetings.Title" HeaderText="Meeting" 
            SortExpression="Meetings.Title" />
        <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" />
        <asp:HyperLinkField DataNavigateUrlFields="FileName" Text="Download" />
    </Columns>
    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
    <SortedAscendingCellStyle BackColor="#F4F4FD" />
    <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
    <SortedDescendingCellStyle BackColor="#D8D8F0" />
    <SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
<h2>Add New</h2>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
    AutoGenerateRows="False" DataSourceID="Docs" DefaultMode="Insert"
     OnItemInserting="DetailsView1_ItemInserting"
     OnItemEditing="DetailsView1_ItemEditing"
    >
    <Fields>
        <asp:BoundField DataField="Documents.Title" HeaderText="Title" 
            SortExpression="Documents.Title" />
        <asp:TemplateField HeaderText="Type" SortExpression="Type">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
                    <asp:ListItem Value="Handout">Handout</asp:ListItem>
                    <asp:ListItem Value="Minutes">Minutes</asp:ListItem>
                    <asp:ListItem Value="Agenda">Agenda</asp:ListItem>
                    <asp:ListItem Value="Other">Other</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:DropDownList ID="DropDownList2" runat="server">
                    <asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
                    <asp:ListItem Value="Handout">Handout</asp:ListItem>
                    <asp:ListItem Value="Minutes">Minutes</asp:ListItem>
                    <asp:ListItem Value="Agenda">Agenda</asp:ListItem>
                    <asp:ListItem Value="Other">Other</asp:ListItem>
                </asp:DropDownList>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Type") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="MeetingID" SortExpression="MeetingID">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="Meetings" 
                    DataTextField="Title" DataValueField="MeetingID">
                </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="Meetings" 
                    DataTextField="Title" DataValueField="MeetingID">
                </asp:DropDownList>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("MeetingID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="FileName" SortExpression="FileName">
            <EditItemTemplate>
                <asp:FileUpload ID="FileUpload2" runat="server" />
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("FileName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
    </Fields>
</asp:DetailsView>
<asp:AccessDataSource ID="Docs" runat="server" 
    DataFile="~/App_Data/Database1.accdb" 
    DeleteCommand="DELETE FROM [Documents] WHERE [DocID] = ?" 
    InsertCommand="INSERT INTO [Documents] ([Title], [Type], [FileName], [MeetingID]) VALUES (@Title, @Type, @FileName, @MeetingID)" 
    SelectCommand="SELECT Documents.DocID, Documents.Title, Documents.FileName, Documents.Type, Meetings.Title, Documents.MeetingID FROM 
    (Documents LEFT OUTER JOIN Meetings ON Documents.MeetingID = Meetings.MeetingID)" 
    UpdateCommand= "UPDATE [Documents] SET [Title] = ?, [Type] = ?, [FileName] = ?, [MeetingID] = ? WHERE [DocID] = ?">
    <DeleteParameters>
        <asp:Parameter Name="DocID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Documents.Title" Type="String" />
        <asp:Parameter Name="Type" Type="String" />
        <asp:Parameter Name="FileName" Type="String" />
        <asp:Parameter Name="MeetingID" Type="Int32" />
    </InsertParameters>
    <SelectParameters>
        <asp:SessionParameter Name="?" SessionField="UserID" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="Title" Type="String" />
        <asp:Parameter Name="Type" Type="String" />
        <asp:Parameter Name="FileName" Type="String" />
        <asp:Parameter Name="MeetingID" Type="Int32" />
        <asp:Parameter Name="DocID" Type="Int32" />
    </UpdateParameters>
</asp:AccessDataSource>
<asp:Label ID="StatusLabel" runat="server" Text="Label"></asp:Label>
<asp:AccessDataSource ID="Meetings" runat="server" 
    DataFile="~/App_Data/Database1.accdb" 
    SelectCommand="SELECT [MeetingID], [Title] FROM [Meetings] WHERE ([AdminID] = ?)">
    <SelectParameters>
        <asp:SessionParameter Name="AdminID" SessionField="UserID" Type="Int32" />
    </SelectParameters>
</asp:AccessDataSource>

我还在上面的其余c#代码中进行了编辑,但时间不长

母版页中的表单元素:

<body style="background-color: rgb(231, 231, 255);height:100%;margin:0px;padding:0px">
<form id="form1" runat="server">

ETA:我发现了问题。我一直在使用一个空文本文件作为测试 - 读取为0字节,使文件上传者感到困惑,认为它没有文件。在文件中添加一些文本使其完美运行。

感谢大家的帮助!

1 个答案:

答案 0 :(得分:8)

我发现了问题。我一直在使用一个空文本文件作为测试 - 读取为0字节,使文件上传者感到困惑,认为它没有文件。在文件中添加一些文本使其完美运行。

关于为什么大小为0字节的文件与其他文件根本没有文件的情况不同的问题。