Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I used FileUpload control on my page.Even after choosing a file ,it shows FileUpload Control's "hasfile" property as false.plz help...

[EDIT, moved from false "solution" post — SA]

XML
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>       

<asp:fileupload id="fileupload" runat="server">
    </ContentTemplate>
</asp:UpdatePanel>



code behind
C#
void btnSave_Click(object sender, EventArgs e)
{
  if (fileupload.HasFile)
    {     
      fileupload.SaveAs(Server.MapPath(FileVirtualPath + fileupload.FileName));
     }
  Save();
}
Posted
Updated 19-Jan-12 14:23pm
v2
Comments
Muralikrishna8811 19-Jan-12 7:51am    
Hi can you plz post your code .

It'll help to findout whats wrong in code
Sergey Alexandrovich Kryukov 19-Jan-12 20:24pm    
Please don't post anything as a solution if it is not a solution. Use "Improve question".
I moved your source code from "solution" to the question.
--SA

Add a trigger for your UpdatePanel
<Triggers>
<asp:PostBackTrigger ControlID="btnSave" />
</Triggers>

This will force a postback when the upload button is clicked.

Also add the line below to the Page_Load
Page.Form.Attributes.Add("enctype", "multipart/form-data");
 
Share this answer
 
I am doing like this in my project and it is working fine.

XML
<asp:UpdatePanel ID="UpdatePanel" runat="server">
   <ContentTemplate>
     <asp:FileUpload ID="uploadProfilePic" runat="server" ClientIDMode="Static"/>


code behind -

C#
HttpPostedFile objHttpPostedFile = uploadProfilePic.PostedFile;
string ProfilePic_fileName = uploadProfilePic.PostedFile.FileName;


Try using this....
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Jan-12 20:59pm    
This looks correct, but it does not explain why OP has the problem.

I suspect (essential part of code in not shown, that's the problem) I spotted what's going wrong -- please take a look at my answer. What do you think?
--SA
It's hard to say what exactly happened, because you still don't show all relevant parts of the code. Importantly, your code sample tells nothing about the button which is supposedly named btnSave and nothing indicates that an event handler btnSave_Click is added to an invocation list of the event instance Click of this button. Is is not shown in this button related to the post of the file or not. Importantly, you don't show the form and its controls.

So, I only can suspect that the problem is related to this button and the event handler: what happens in this event handler happens before posting of the file, or file is not posted at all.

If you look at the Microsoft code sample, everything is clear: forms is shown, one can see what triggers the post. You should also do it in a similar way, please see: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.hasfile.aspx[^].

—SA
 
Share this answer
 
Comments
Member 2099872 27-Feb-13 18:28pm    
It still does not work in windows XP environment!
Arun Nandy
Hi, you can also try the below code...

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); // ADD THE SCRIPT MANAGER ON CONTENT OR MASTER PAGE

scriptManager.RegisterPostBackControl(this.btnSubmit); // SUBMIT BUTTON ON THE PAGE
 
Share this answer
 
Just register the Button control as a PostBack Trigger for the UpdatePanel

<asp:postbacktrigger controlid="btnUpload" xmlns:asp="#unknown">
<asp:postbacktrigger controlid="btnRemove" xmlns:asp="#unknown">


This should force the button to do a fullpost back on the page
 
Share this answer
 
Hi,
I have a solution for this,but how it will be usefull to u i dont no,for upload the file am using this code,It is working fine for me.
This should be write in button click event,the upload path can be given like this

if (Fileupload.HasFile)
{
string inputfileName = Fileupload.FileName;

Fileupload.SaveAs(Server.MapPath("~/filename contained folder name/")+ inputfileName);

}
 
Share this answer
 
soluation 5 solved problem but you must write :
<triggers><asp:postbacktrigger controlid="btnSave" xmlns:asp="#unknown" />
after :
<asp:updatepanel id="UpdatePanel1" runat="server">

and build&run you code .
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900