File Upload Control

Last post 07-07-2009 8:58 AM by prateek1. 14 replies.

Sort Posts:

  • File Upload Control

    07-04-2009, 8:38 AM
    • Member
      10 point Member
    • shahid.majeed
    • Member since 10-07-2008, 8:24 AM
    • Lahore
    • Posts 69

    Hi friends i have a error in my file upload control. i select the file that i want to upload, but when i click the upload button. its give the this error. i dont know why this error come.

    Error:

    Object reference not set to an instance of  an object.

    here is my Upload Button Code code

    protected void Button2_Click(object sender, EventArgs e)
        {
            string strFileName;
            strFileName = FileField.PostedFile.FileName;
            string c = System.IO.Path.GetFileName(strFileName);
            try
            {
                Image2.Visible = true;
                FileField.PostedFile.SaveAs("C:\\UploadedUserFiles\\" + c);
                Image2.Visible = false;
                Label2.Visible = true;
            }
            catch (Exception exp)
            {
                Label2.Text = exp.Message.ToString();
                Label2.Visible = true;
            }
        }


    Here is My page Code

    <table cellpadding="0" cellspacing="0" class="style8">
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    *Name:</td>
                                                <td width="275px">
                                                    <asp:TextBox ID="TextBox2" runat="server" Width="250px"></asp:TextBox>
                                                    </td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                     </td>
                                            </tr>
                                            <tr>
                                               <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    *Email Address:</td>
                                                <td width="275px">
                                                    <asp:TextBox ID="TextBox4" runat="server" Width="250px"></asp:TextBox>
                                                    </td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                     </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    *Contact Number:</td>
                                                <td width="275px">
                                                   <asp:TextBox ID="TextBox5" runat="server" Width="250px"></asp:TextBox> </td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                     </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    *City:</td>
                                                <td width="275px">
                                                    <asp:TextBox ID="TextBox6" runat="server" Width="250px"></asp:TextBox></td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                     </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    Your CV:</td>
                                                <td width="275px">
                                                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                                        <ContentTemplate>
                                                            <asp:FileUpload ID="FileField" runat="server" Width="247px" /><br />
                                                            <asp:Button ID="Button2" runat="server" Text="Upload" Height="20px" 
                                                                onclick="Button2_Click" /><asp:Image ID="Image2" runat="server" Visible="False" />
                                                            <asp:Label ID="Label2" runat="server" Font-Names="Arial" Font-Size="10pt" 
                                                                ForeColor="#535353" Text="File Uploaded Successfully" Visible="False"></asp:Label>
                                                        </ContentTemplate>
                                                    </asp:UpdatePanel>
                                                    
                                                </td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                    </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" 
                                                    width="150px" valign="top">
                                                    Describe Yourself:                              <td width="275px">
                                                    <asp:TextBox ID="TextBox8" runat="server" Width="250px" Height="200px"></asp:TextBox></td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" 
                                                    width="150px" valign="top">
                                                     </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    </td>
                                                <td width="275px">
                                                    <asp:Button ID="Button1" runat="server" Text="Apply" 
                                                        onclick="Button1_Click" />
                                                    </td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                    </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: left; height: 30px; padding-left: 10px;" 
                                                    colspan="3">
                                                    <asp:Label ID="Label1" runat="server" ForeColor="#009999" 
                                                        Text="* Your Resume Send Successfully." Visible="False"></asp:Label>
                                                    </td>
                                            </tr>
                                        </table>


    Thanks in Advance

    Regards,
    Shahid Majeed
    Email: shahid.majeed@gmail.com
  • Re: File Upload Control

    07-04-2009, 9:09 AM

    shahid.majeed:

  •  <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
  •                                                     <ContentTemplate>  
  •                                                         <asp:FileUpload ID="FileField" runat="server" Width="247px" /><br />  
  •                                                         <asp:Button ID="Button2" runat="server" Text="Upload" Height="20px"   
  •                                                             onclick="Button2_Click" /><asp:Image ID="Image2" runat="server" Visible="False" />  
  •                                                         <asp:Label ID="Label2" runat="server" Font-Names="Arial" Font-Size="10pt"   
  •                                                             ForeColor="#535353" Text="File Uploaded Successfully" Visible="False"></asp:Label>  
  •                                                     </ContentTemplate>  
  •                                                 </asp:UpdatePanel>  
  • File upload control would not work with update panel (known issue)... to make it work you need to have a button with registered as asynchrinous postback..

    check out some reference thread > UpdatePanel and FileUpload - ASP.NET Forums

    Fileupload inside updatepanel problems - ASP.NET Forums

    File Upload Control is not working in Update Panel - ASP.NET Forums

    hope it helps./.


    नमस्ते,
    [KaushaL] || BloG || MS MVP

    "I would love to change the world, but they won’t give me the source code"


    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
  • Re: File Upload Control

    07-04-2009, 9:10 AM
    • Contributor
      5,610 point Contributor
    • integrasol
    • Member since 06-05-2009, 3:18 PM
    • Posts 1,041

    Skip using the PostedFile property and just use SaveAs. Once the file has been uploaded (using SaveAs) you can use the PostedFile property.

    Thanks

    Carsten

    Please mark this post as Answer if it is of help to you. :-)
  • Re: File Upload Control

    07-04-2009, 9:17 AM
    • All-Star
      57,651 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,201
  • Re: File Upload Control

    07-04-2009, 9:45 AM
    Answer

    Do this:

    <asp:UpdatePanel runat="server">

                <ContentTemplate>              

                    <asp:FileUpload runat="server" />

                    <asp:Button runat="server" Text="Upload" />

                </ContentTemplate>

                <Triggers>

                    <asp:PostBackTrigger ControlID="btnUpload" />               

                </Triggers>

            </asp:UpdatePanel>

     protected void btnUpload_Click(object sender, EventArgs e)

        {

            string filename = System.IO.Path.GetFileName(fuUpload.FileName);

            fuUpload.SaveAs("C:\temp" + filename);

        }

    Here is good discussion on file upload in AJAX Update Panel.

    http://forums.asp.net/p/1105208/1689084.aspx

    Regards,
    Ram Reddy Mekha, +91-994-840-4315
    http://abhiramreddymekha.blogspot.com

    Please Mark as Answer if the post helps you.
  • Re: File Upload Control

    07-04-2009, 10:01 AM
    • Member
      10 point Member
    • shahid.majeed
    • Member since 10-07-2008, 8:24 AM
    • Lahore
    • Posts 69

    i do with the triger, its upload the file at destination. now i want to rename the user file before uploading. because my be some one already save the file with the same file name. so please let me know how i can do this.


    Thanks

    Regards,
    Shahid Majeed
    Email: shahid.majeed@gmail.com
  • Re: File Upload Control

    07-04-2009, 10:27 AM
    • All-Star
      57,651 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,201
  • Re: File Upload Control

    07-04-2009, 11:51 PM

    shahid.majeed:
    because my be some one already save the file with the same file name.

    There are possible 2 alternatives.. whenever you upload a file:

    [1] just check whether file exists or not in the folder where you want to save the file. IF it exists then simply delete it first before saving the file:


            if (System.IO.File.Exists(Server.MapPath("~/images/test.jpg")))
                System.IO.File.Delete(Server.MapPath("~/images/test.jpg"));
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/images/test.jpg"));


    OR

    [2] save the file with some other name (renaming the file with some other name as you want to do)

    you can save the file with any/proper file naming convension with PostedFile.SaveAs() method as:


            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/images/test.jpg"));



    hope it helps./.

    नमस्ते,
    [KaushaL] || BloG || MS MVP

    "I would love to change the world, but they won’t give me the source code"


    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
  • Re: File Upload Control

    07-05-2009, 4:56 AM
    • Member
      10 point Member
    • shahid.majeed
    • Member since 10-07-2008, 8:24 AM
    • Lahore
    • Posts 69

    when i am going to rename the file name,  but its not uploading the file. even did not give me any error. please let me know what is the problem in my code.


    Here is my Code

    protected void Button2_Click(object sender, EventArgs e)
        {
           //find upload directory
            string UpPath;
            UpPath = Server.MapPath("CV");
            
            // Renaming the file name
            string strFileName;
            strFileName = FileField.PostedFile.FileName;
            string c = System.IO.Path.GetFileName(strFileName);
            c = this.Session["JobNo"].ToString()+ c; 
            
            //Uploading the CV
            try
            {
                Image2.Visible = true;
                FileField.PostedFile.SaveAs(UpPath + c);
                Image2.Visible = false;
                Label2.Visible = true;
            }
            catch (Exception exp)
            {
                Label2.Text = exp.Message.ToString();
                Label2.Visible = true;
            }
        }


    Page Code

    <table cellpadding="0" cellspacing="0" class="style8">
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    *Name:</td>
                                                <td width="275px">
                                                    <asp:TextBox ID="TextBox2" runat="server" Width="250px"></asp:TextBox>
                                                    </td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                     </td>
                                            </tr>
                                            <tr>
                                               <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    *Email Address:</td>
                                                <td width="275px">
                                                    <asp:TextBox ID="TextBox4" runat="server" Width="250px"></asp:TextBox>
                                                    </td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                     </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    *Contact Number:</td>
                                                <td width="275px">
                                                   <asp:TextBox ID="TextBox5" runat="server" Width="250px"></asp:TextBox> </td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                     </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    *City:</td>
                                                <td width="275px">
                                                    <asp:TextBox ID="TextBox6" runat="server" Width="250px"></asp:TextBox></td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                     </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    Your CV:</td>
                                                <td width="275px">
                                                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                                        <ContentTemplate>
                                                            <asp:FileUpload ID="FileField" runat="server" Width="247px" /><br />
                                                            <asp:Button ID="Button2" runat="server" Text="Upload" Height="20px" 
                                                                onclick="Button2_Click" /><asp:Image ID="Image2" runat="server" Visible="False" />
                                                            <asp:Label ID="Label2" runat="server" Font-Names="Arial" Font-Size="10pt" 
                                                                ForeColor="#535353" Text="File Uploaded Successfully" Visible="False"></asp:Label>
                                                        </ContentTemplate>
                                                        <Triggers>
                                                            
                                                            <asp:PostBackTrigger ControlID = "Button2" />
                                                        </Triggers>
                                                    </asp:UpdatePanel>
                                                    
                                                </td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                    </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" 
                                                    width="150px" valign="top">
                                                    Describe Yourself:                              <td width="275px">
                                                    <asp:TextBox ID="TextBox8" runat="server" Width="250px" Height="200px"></asp:TextBox></td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" 
                                                    width="150px" valign="top">
                                                     </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: right; padding-right: 10px; height: 30px" width="150px">
                                                    </td>
                                                <td width="275px">
                                                    <asp:Button ID="Button1" runat="server" Text="Apply" 
                                                        onclick="Button1_Click" />
                                                    </td>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #FF0000; padding-left: 10px; text-align: left" width="150px">
                                                    </td>
                                            </tr>
                                            <tr>
                                                <td style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; color: #535353; text-align: left; height: 30px; padding-left: 10px;" 
                                                    colspan="3">
                                                    <asp:Label ID="Label1" runat="server" ForeColor="#009999" 
                                                        Text="* Your Resume Send Successfully." Visible="False"></asp:Label>
                                                    </td>
                                            </tr>
                                        </table>

    Thanks 

    Regards,
    Shahid Majeed
    Email: shahid.majeed@gmail.com
  • Re: File Upload Control

    07-05-2009, 11:21 AM
    Answer

    shahid.majeed:
     FileField.PostedFile.SaveAs(UpPath + c);  

    just a small mistake..

    you need to combine the path as..

    FileField.PostedFile.SaveAs(System.IO.Path.Combine(UpPath, c));


    hope it helps./.

    नमस्ते,
    [KaushaL] || BloG || MS MVP

    "I would love to change the world, but they won’t give me the source code"


    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
  • Re: File Upload Control

    07-06-2009, 7:28 AM
    • Member
      10 point Member
    • shahid.majeed
    • Member since 10-07-2008, 8:24 AM
    • Lahore
    • Posts 69

    i have some doudth in my mind. i am renaming the file. for example file name is abc.doc and i rename it with the code 1011abc.doc , butt at client computer file name remain abc.doc. when fileupload control try to upload the file with 1011abc.doc it did not find on the client computer. 


    Thanks.

    Regards,
    Shahid Majeed
    Email: shahid.majeed@gmail.com
  • Re: File Upload Control

    07-06-2009, 9:01 AM

    shahid.majeed:
    i have some doudth in my mind. i am renaming the file. for example file name is abc.doc and i rename it with the code 1011abc.doc , butt at client computer file name remain abc.doc. when fileupload control try to upload the file with 1011abc.doc it did not find on the client computer. 

    try out yourself... it should work.. SaveAs() method will save the object that resides as FileUpload.PostedFile with given name..

    नमस्ते,
    [KaushaL] || BloG || MS MVP

    "I would love to change the world, but they won’t give me the source code"


    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
  • Re: File Upload Control

    07-07-2009, 6:00 AM
    • Member
      240 point Member
    • prateek1
    • Member since 07-01-2009, 7:52 AM
    • Posts 47

    jst use this script:

    <script language="VB" runat="server">

    Sub Upload(Source As Object, e As EventArgs)

       If Not (myFile.PostedFile Is Nothing) Then

          Dim intFileNameLength as Integer
          Dim strFileNamePath as String
          Dim strFileNameOnly as String

          'Logic to find the FileName (excluding the path)
          strFileNamePath = MyFile.PostedFile.FileName
          intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
          strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)

                'myFile.PostedFile.SaveAs("c:\inetpub\wwwroot\yourwebapp\upload\" & strFileNameOnly)
                myFile.PostedFile.SaveAs("d:\" & strFileNameOnly)
          lblMsg.Text = "File Upload Success."

       End If
    End Sub
    </script>


    and call this script on button click and ur problem is solved

    if ur problem is solved marked as answer plz

    Regard

    Prateek


    Regard

    Prateek Bhatnagar (BCA & MCA)

    Prateek'Tips


    www.msdotnetheaven.com

  • Re: File Upload Control

    07-07-2009, 6:07 AM
    • Star
      11,839 point Star
    • shahed.kazi
    • Member since 07-09-2008, 2:15 AM
    • Sydney, Australia
    • Posts 2,328

    Not sure what the problem is. You are uploading a file from the client and saving it to your server. You can choose to rename the filename and save it to any folder as long as the user running the web app has write access to the directory.

  • Re: File Upload Control

    07-07-2009, 8:58 AM
    • Member
      240 point Member
    • prateek1
    • Member since 07-01-2009, 7:52 AM
    • Posts 47

    yes i upload file from client side and save that file into server side

    if that not solve your  requirement then send reply again what is ur need ?


    i m try my best to solve your problem

    Regard

    Prateek

    Regard

    Prateek Bhatnagar (BCA & MCA)

    Prateek'Tips


    www.msdotnetheaven.com

Page 1 of 1 (15 items)