Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I'm working on file upload, but i dont know why its not taking any files.

i have added fileupload in asp.net page with upload button,

i select one file n click upload button, on button its i get the fileone.hasfile as empty.

my code:

C#
protected void btnupload_Click(object sender, EventArgs e)
        {
            if (PhotoUpload1.HasFile)
            {
                Session["ImageBytes"] = PhotoUpload1.FileBytes;
                string str = Server.MapPath(PhotoUpload1.FileName);
                ImagePreview.ImageUrl = "~/DriverSetup/" + PhotoUpload1.FileName;
            }

        }


its not going into if loop, just return false;

plz suggest me,

update panel code:

<pre lang="xml"><asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                      <ContentTemplate>
                                      <asp:Image CssClass="img img-thumbnail" Width="150px" Height="180px" ID="ImagePreview" runat="server" />
                                      <br />
                                      <asp:FileUpload ID="PhotoUpload1" runat="server" />
                                       <br />
                                       <asp:Button ID="btnupload" runat="server" Text="Upload"
                                          onclick="btnupload_Click" />
                                          <asp:UpdateProgress ID="UpdateProgress1" runat="server">
                                          <ProgressTemplate>Please wait...</ProgressTemplate>
                                          </asp:UpdateProgress>
                                      </ContentTemplate>
                                      <Triggers>
                                      <asp:PostBackTrigger ControlID="btnupload" />
                                      </Triggers>
                                      </asp:UpdatePanel>

</pre>


best regards
Posted
Updated 21-Jan-14 22:18pm
v2
Comments
JoCodes 22-Jan-14 2:22am    
Are you using update panel?
abdul subhan mohammed 22-Jan-14 2:27am    
no
abdul subhan mohammed 22-Jan-14 2:38am    
what if i'm using update panel??
how can i use fileupload in updatepanel with trigger
abdul subhan mohammed 22-Jan-14 2:58am    
thanks, dude... i got it, u was right, i was using update panel in d master page. & it was content page where i was using fileupload.

Any ways, thanks once again.

But i want to use update panel with file upload and upload button.

any suggestions plzzz...

JoCodes 22-Jan-14 3:51am    
You should use triggers for the update panel. Its a common error.

try this

C#
protected void UploadButton_Click(object sender, EventArgs e)
{
    if(FileUploadControl.HasFile)
    {
        try
        {
            if(FileUploadControl.PostedFile.ContentType == "image/jpeg")
            {
                if(FileUploadControl.PostedFile.ContentLength < 102400)
                {
                    string filename = Path.GetFileName(FileUploadControl.FileName);
                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                    StatusLabel.Text = "Upload status: File uploaded!";
                }
                else
                    StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
            }
            else
                StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
        }
        catch(Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}
 
Share this answer
 
Its very common issue when we use FileControl with UpdatePanel. Should use Triggers.

Refer

http://stackoverflow.com/questions/9682737/fileupload-hasfile-is-always-false[^]
 
Share this answer
 
Comments
abdul subhan mohammed 22-Jan-14 3:58am    
thanks for your help, but it refreshing whole page, which i dont want to... any suggestion??
JoCodes 22-Jan-14 4:04am    
Can you show me the code?
Add this in your update panel, ASPX page

ASP.NET
<Triggers>
            <asp:PostBackTrigger ControlID="btnupload" />
        </Triggers>
    </asp:UpdatePanel>
 
Share this answer
 
Comments
abdul subhan mohammed 22-Jan-14 4:24am    
its their in my code, u can c last lines...
Karthik_Mahalingam 22-Jan-14 4:29am    
try with AsyncPostBackTrigger
abdul subhan mohammed 22-Jan-14 4:33am    
then fileupload is not working...
Karthik_Mahalingam 22-Jan-14 4:42am    
what error u r facing actually ?
abdul subhan mohammed 22-Jan-14 4:46am    
i'm not facing any error,
if i use postbacktrigger it refreshing my whole page, n
if i use asynpostbacktrigger then fileupload is not working...

i dont want to refresh whole on image upload ...

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

  Print Answers RSS


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