Click here to Skip to main content
14,424,431 members
Rate this:
Please Sign up or sign in to vote.
See more:
hello,
i am using a fileupload control in my webform but when i select any file in fileupload control then its property
Fileupload1.HasFile showing false what is the problem my code is

if(FileUpload1.HasFile)
        {
            string filename = FileUpload1.FileName; 
            string imagepath = "Uploads/" + filename;
            FileUpload1.SaveAs(Server.MapPath("Uploads/" + filename));
            values[4] = filename;
            values[5] = imagepath;
            obj_msg.fun_Insertmessagewithattachment(values);
        }


just because if(FileUpload1.HasFile) this condition is not true my code is not running
Pleas Help me....

Thanks & Regards
Srishti Gupta
Posted
Updated 18-Jun-13 0:40am
v2
Comments
Zafar Sultan 18-Jun-13 5:42am
   
Can you post complete code you are using to upload the file? I mean maybe you are using Button Click event. Can you show that?
srishti056 18-Jun-13 5:48am
   
ya sure this is my complete code

protected void btnsend_Click(object sender, EventArgs e)
{
string uname=txtto.Text;
string subject = txtsub.Text;
string msg = Editor1.Content;
int id= obj_msg.fun_inv_selectuserid(uname,ref dt);
Label1.Text = "";
values[0] = Convert.ToString(userid);
values[1] = Convert.ToString(id);
values[2] = subject;
values[3] = msg;
//if (FileUpload1.PostedFile == null && FileUpload1.PostedFile.FileName == "")
//{
// obj_msg.fun_Insertmessage(values);
//}
if(uploadimg.HasFile)
{
string filename = uploadimg.FileName;
string imagepath = "Uploads/" + filename;
uploadimg.SaveAs(Server.MapPath("Uploads/" + filename));
values[4] = filename;
values[5] = imagepath;
obj_msg.fun_Insertmessagewithattachment(values);
}

else
{
obj_msg.fun_Insertmessage(values);
}
txtto.Text = " ";
txtsub.Text = " ";

}
   
Is your FileUpload inside the UpdatePanel?
Rate this:
Please Sign up or sign in to vote.

Solution 1

Any time it will be happen when working with file upload control and the best solution is to use update panel to reduce this kind of error. I hope its will be working properly...:-)

<table width="100%" align="center" cellpadding="0" cellspacing="2">
                                       <tr>
                                           <td class="style2">
                                               Enter Access Path :
                                           </td>
                                           <td>
                                               &nbsp; &nbsp;
                                               <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                               <ContentTemplate>
                                               <asp:FileUpload ID="FileUpload1" runat="server" />
                                               <asp:Button ID="cmd_getpath" runat="server" OnClick="cmd_getpath_Click" Text="Get Path" />
                                               <asp:TextBox ID="txtPath" runat="server"></asp:TextBox>
                                               </ContentTemplate>
                                                   <Triggers >
                                                   <asp:PostBackTrigger ControlID="cmd_getpath" />
                                                   </Triggers>
                                               </asp:UpdatePanel>
                                           </td>
                                       </tr>
</table>
   
v2
Comments
srishti056 18-Jun-13 6:12am
   
ok i try
srishti056 18-Jun-13 6:30am
   
thank u so much its working
Chintan Desai1988 18-Jun-13 7:15am
   
u welcome dear.....:-)
Rate this:
Please Sign up or sign in to vote.

Solution 2

Problem

Refer - FileUpload Class[^].
Quote:
The FileUpload control is designed to be used only in postback scenarios and not in asynchronous postback scenarios during partial-page rendering. When you use a FileUpload control inside an UpdatePanel[^] control, the file must be uploaded by using a control that is a PostBackTrigger[^] object for the panel.
.
Solution


  • Add PostBackTrigger for FileUpload1.
    <Triggers>
        <asp:Postbacktrigger controlid="FileUpload1" />
    </Triggers>
  • Use FileName property to check whether the File is present or not.
    if (FileUpload1.FileName != string.Empty)
    {
          //do something
    }

   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100