views:

140

answers:

3
+1  Q: 

File Upload Error

Hi!! 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;

    }  


}
+1  A: 

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.

womp
S.. I tried as u said. Same probem..
Nila
Hi!! I found the problem with this code. Actually, I'm using another button in the same web page, so, the file upload control is getting refreshed for that button too. Now, wat is the solution for this???
Nila
A: 

As far as I know, the FileUpload control prevents the upload of a file without the use of its own "browse..." button. This to prevent misuse of the clients filesystem by auto-uploading files from it.

The only solution is to use a layer over the control that displays a nicer view but preserving the possibility to use the browse button or type a path manually.

You can find an extended solution on how to style an FileUpload here: Link

Webleeuw
A: 

Nothing works for me.. Problem is that I'm using three more buttons in the same page. The other buttons initializing the file upload control. So, when clicking the upload button, the file name is empty. So, I used another page for uploading the word document. Now, it is working.. !!

Nila