C# – File Upload Error

asp.netcfile upload

This is my partial code of fileupload control page. This is the one I'm using. When uploading the file, the filename, postedfile, everything is empty.I tried ajax file upload too. It is showing the error, "Object reference not set to an instance". Wat is the problem with my coding?

<table>
    <tr>
        <td align="center">
            <span class="txt">
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                       <asp:FileUpload ID="fpResumenew" runat="server" Visible="false" Width="226px" />
                    </ContentTemplate>                     
                </asp:UpdatePanel>
            </span>
        </td>
    </tr>
    <tr>
        <td align="center">
            &nbsp;
        </td>
    </tr>
    <tr>
        <td style="vertical-align: top" align="center">
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <asp:Button ID="btnUpload" Font-Bold="true" 
                     DisabledText="Processing..." Visible="false"
                     Text="Upload" BackColor="Maroon" ForeColor="White" runat="server" OnClick="btnUpload_Click" />
                </ContentTemplate>
                <Triggers>
                        <asp:PostBackTrigger ControlID="btnUpload" />
                    </Triggers>
            </asp:UpdatePanel>
        </td>
    </tr>
</table>

protected void btnUpload_Click(object sender, EventArgs e)

{

    string strname = fpResumenew.FileName.ToString();

    if (fpResumenew.PostedFile.FileName.Trim().Length != 0)

    {


                byte[] binary = new byte[fpResumenew.PostedFile.ContentLength];
                binary = fpResumenew.FileBytes;
                string doc = fpResumenew.FileName;
                string contenttype = fpResumenew.PostedFile.ContentType;
                objservice1.UpdateResume(int.Parse(Session["LoginId"].ToString()), doc, binary, contenttype);
                Response.Redirect("delresume.aspx?Action=U");


    }
    else
    {
        lblmsg.Text = "File is not Found";
        lblmsg.Visible = true;

    }  


}

Best Answer

Try adding the

<Triggers>
   <asp:PostBackTrigger ControlID="btnUpload" />
</Triggers>

declaration to your first update panel, not the second one. The trigger needs to be in the panel that contains the FileUpload control, not the one that contains the button. The PostBackTrigger forces the fileupload to be posted back regularly, which is what it needs to satisfy browser security requirements.