Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm using to upload an image using image control.its is working and uploading image but when i run in visual studio its work good but when i publish it is saving image but not showing..
cant understnad why it is happening..
please help me




C#
protected void Page_Load(object sender, EventArgs e)
    {
        //Session.Timeout = 90;

        string username = (string)(Session["UserAuthentication"]);
            if(Session["UserAuthentication"]!=null)
            {
                Label2.Text=username;
                Image1.ImageUrl = "Images/" + username;



            }



C#
private void StartUpLoad()
       {
           //get the file name of the posted image
              string username = (string)(Session["UserAuthentication"]);

              string imgName = username;
           //sets the image path
              string imgPath = "Images/" + imgName;


           //get the size in bytes that

           int imgSize = FileUpload1.PostedFile.ContentLength;

           //validates the posted file before saving
           if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
           {
               // 10240 KB means 10MB, You can change the value based on your requirement
               if (FileUpload1.PostedFile.ContentLength > 1024000)
               {
                   Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);

               }
               else
               {
                   //then save it to the Folder
                   FileUpload1.SaveAs(Server.MapPath(imgPath));
                   Image1.ImageUrl = "~/images/"+imgName;
                   Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);

               }

           }


C#
protected void Button2_Click1(object sender, EventArgs e)
       {
               StartUpLoad();
           
       }
Posted
Comments
Ahmed Bensaid 12-Jun-13 3:50am    
http://www.codeproject.com/Questions/360873/file-upload-working-on-local-host-but-not-working
fak_farrukh 12-Jun-13 4:09am    
still the same result

1 solution

Use below StartUpload() Method

C#
private void StartUpLoad()
       {
           //get the file name of the posted image
              string username = (string)(Session["UserAuthentication"]);

           //get the size in bytes that

           int imgSize = FileUpload1.PostedFile.ContentLength;

           //validates the posted file before saving
           if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
           {
               // 10240 KB means 10MB, You can change the value based on your requirement
               if (FileUpload1.PostedFile.ContentLength > 1024000)
               {
                   Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);

               }
               else
               {
                   //then save it to the Folder
                string[] splitter = FileUpload1.PostedFile.FileName.Split('.');
                string filePath = "Images/" + username +"." + splitter[1].ToString();
                FileUpload1.SaveAs(Server.MapPath(filePath));
                Image1.ImageUrl = "~/" + filePath;
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);

               }

           }
 
Share this answer
 
Comments
fak_farrukh 12-Jun-13 6:26am    
what should be written on page load????

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