Click here to Skip to main content
15,877,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I have an image tag and a fileuploader to upload file.It works fine .Sometimes the user need to change the already uploaded file.It also works fine .But the problem is that even after uploading new image ,it is saved ,but the earlier image is still showing on image tag and is not refreshed.Any help will be really appreciated .Thanks in Advance.

What I have tried:

<tr>
                                                           <td align="left">Choose file </td>
                                                           <td style="padding-left:5px; padding-right:5px;">:</td>
                                                           <td align="left">
                                                               <asp:FileUpload ID="FileUpload1" runat="server" class="txtboxAll" />
                                                           </td>
                                                       </tr>

                                               </table>
                                           </td>
                                       </tr>
                                       <tr>
                                           <td align="center">
                                               <asp:Button ID="but_uploadfile" runat="server" ValidationGroup="First"  TabIndex="16"
                                               CssClass="btnAll" Text="Upload" onclick="but_uploadfile_Click" />
                                               <asp:Button ID="btn_Close01" runat="server" ValidationGroup="First" TabIndex="17"
                                               CssClass="btnAll" Text="Close" />
                                           </td>
                                       </tr>



protected void but_uploadfile_Click(object sender, EventArgs e)
        {
            queryusrid = Request.QueryString["cid"].ToString();
            string oldfilename1 = Path.GetFileName(FileUpload1.FileName);
            //FileInfo file = new FileInfo(FileUpload1.FileName);
            ////var sizeInBytes = file.Length;

            //Bitmap img = new Bitmap(FileUpload1.FileName);

            //var imageHeight = img.Height;
            //var imageWidth = img.Width;
            System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
            int height = img.Height;
            int width = img.Width;

            if (oldfilename1 == "")
            {
                Alert.Show("Please select a file to upload");
                return;
            }
            else
            {
                string gid2 = lbl_gid.Text;
                string ext = Path.GetExtension(FileUpload1.FileName);
                if (ext == ".jpg" || ext == ".JPG" || ext == ".jpeg" || ext == ".JPEG")
                {
                    //if (height >= 180 && height <= 250 && width >= 200 && width <= 250)
                    if (width == 200 && height == 230)
                    {


                        string FileExist = objcon.GetDataReadervalue("select IN005_01_01  from IN005_01 where IN005_01='" + gid2 + "' and IN005_01_07='photo'", "value");
                        if (FileExist == "Not Found")
                        {

                            string serpath1 = Server.MapPath("~/Registeration/ProfilePics/");
                            string newfileid1 = GenerateFileID();
                            FileUpload1.SaveAs(serpath1 + newfileid1 + ext);
                            string newfileid4 = newfileid1 + ext;
                            bool ret3 = objcon.Modify("insert into IN005_01(IN005_01_01,IN005_01_02,IN005_01_03,IN005_01_06,IN005_01_07,IN005_01)Values('" + newfileid1 + "','" + newfileid4 + "','" + oldfilename1 + "','sumhr','photo','" + gid2 + "')");
                            DataList1.DataBind();
                            UpdatePanel7.Update();

                        }
                        else
                        {
                            string filename = objcon.GetDataReadervalue("select IN005_01_02  from IN005_01 where IN005_01='" + gid2 + "' and IN005_01_07='photo'", "value");
                            string filenameetn = objcon.GetDataReadervalue("select IN005_01_01  from IN005_01 where IN005_01='" + gid2 + "' and IN005_01_07='photo'", "value");
                            string path = Server.MapPath("~") + "\\Registeration" + "\\ProfilePics" + "\\" + filename;
                            if (System.IO.File.Exists(path))
                            {
                                System.IO.File.Delete(path);
                                string serpath1 = Server.MapPath("~/Registeration/ProfilePics/");
                                string newfileid1 = filenameetn;
                                FileUpload1.SaveAs(serpath1 + newfileid1 + ext);
                                string newfileid4 = newfileid1 + ext;
                                bool ret3 = objcon.Modify("update IN005_01 set  IN005_01_02='" + newfileid4 + "',IN005_01_03='" + oldfilename1 + "',IN005_01_06='auto' where IN005_01_01='" + filenameetn + "'");
                                //grd_InsEdit.DataBind();
                                DataList1.DataBind();
                                UpdatePanel7.Update();

                            }
                        }
                    }
                    else
                    {
                        Alert.Show("Image size should Width*Height-(200*230px)");
                        return;
                    }
                }
                else
                {
                    Alert.Show("Please upload any jpg,png,jpeg files");
                    return;
                }
            }
        }
Posted
Updated 18-Jan-18 21:56pm
v2
Comments
[no name] 19-Jan-18 2:33am    
Are you using ajax async uploader or file upload control and are using update panel.
F-ES Sitecore 19-Jan-18 4:18am    
The browser is showing the cached version of the image. One hack to get around that is to add a random query parameter to the image url;

myImage.Src = "image.jpg?r=" + DateTime.Now.Ticks.ToString();
[no name] 22-Jan-18 0:24am    
Are you using file uploader inside the Update panel.Please update your ASP code above.

1 solution

Please mention which browser you are using.If you are using IE then it cache the old image hence you need to write
Response.Cache.SetCacheability(HttpCacheability.NoCache);
code block before updating the panel.In firefox and chrome it should update.
Reference:Image is not refreshed in updatePanel.Update | The ASP.NET Forums[^]
Another thread[^]
Image upload not working in updatepanel | The ASP.NET Forums[^]
 
Share this answer
 
v2
Comments
Member 12926744 19-Jan-18 23:28pm    
I am using google chrome

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