Click here to Skip to main content
15,896,606 members

Image not getting refreshed In master page using async image upload ajax toolkit

gaurish thakkar asked:

Open original thread
I am using a master page and i have a code the image gets uploaded but the image preview is shown of the previous image .


but when i inspect the source i get the new image preview in the web developer console .


both the new and old image has the same name .

MAy be the cached preview is shown .

can anyone help me



XML
<myasp:UpdatePanel ID="UpdatePanel1" runat="server">
                               <ContentTemplate>
                                   <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnUploadedComplete="AsyncFileUpload1_UploadedComplete"
                                       OnClientUploadStarted="UpLoadStarted" OnClientUploadError="UploadError" OnClientUploadComplete="UploadComplete" />
                               </ContentTemplate>
                           </myasp:UpdatePanel>

                           <myasp:UpdatePanel ID="UpdatePanel2" runat="server">
                               <ContentTemplate>
                                   <div class="thumbnail_pic_box">
                                       <asp:Image ID="imgproj" runat="server" alt="Profile" title="Profile" ImageUrl="~/uploads/NO_IMAGE.jpg" />
                                   </div>
                               </ContentTemplate>

                               </myasp:UpdatePanel>
                            <div id="diverror" >
                                   <asp:Label ID="Label1" runat="server" class="errorproj" ></asp:Label>
                               </div>










XML
function showImage(imagePath)
        {
            alert(imagePath);
            var a = document.getElementById('<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("imgproj").ClientID %>');
            alert(a);
            $('#'+'<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("imgproj").ClientID %>', window.parent.document).attr("src", imagePath); //imgproj

        }
        function clearContents() {
            var AsyncFileUpload = document.getElementById('<%=Page.Master.FindControl("ContentPlaceHolder1").FindControl("AsyncFileUpload1").ClientID%>');
            var txts = AsyncFileUpload.getElementsByTagName("input");
            for (var i = 0; i < txts.length; i++) {
                //type == "file" is what is needed to clear the value
                if (txts[i].type == "hidden" || txts[i].type == "file") {
                    txts[i].value = "";
                }
            }
            $get("<%= Label1.ClientID %>").innerHTML = "";
        }
        function errorMsgFunction(sender, args) {
            $('#diverror', window.parent.document).show();
            $('#diverror', window.parent.document).fadeOut(2000);

            //            $('.errorproj', window.parent.document).fadeIn(4000, function()
            //            {

            //                $('.errorproj', window.parent.document).fadeOut(2000, function()
            //                {
            //                    $('.errorproj', window.parent.document).remove();
            //                });
            //            });

            //            $('#diverror', window.parent.document).fadeIn(500, function()  {
            //                $('#diverror', window.parent.document).fadeOut('slow');

            //            });

        }

        var ifIgnoreError = false;
        function UpLoadStarted(sender, e) {
            var fileName = e.get_fileName();

            var fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
            if (fileExtension == 'jpg' || fileExtension == 'jpeg' || fileExtension == 'gif' || fileExtension == 'GIF' || fileExtension == 'png' || fileExtension == 'PNG') {

                //file is good -- go ahead

            }
            else {
                //stop upload
                ifIgnoreError = true;
                sender._stopLoad();
                alert('error');
            }
        }
        //Function to Show if Error Occurred
        function UploadError(sender, e) {
            if (ifIgnoreError) {

                $('#' + '<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("Label1").ClientID %>').text('Incorrect File Format').fadeIn();
                $("#diverror").fadeIn(4000, function() {

                    $(this).fadeOut(6000, function() {
                        clearContents();
                        $('#'+'<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("imgproj").ClientID %>', window.parent.document).attr("src", "uploads/NO_IMAGE.jpg");

                    });
                });
            }

            else {
                alert(e.get_message());
            }
        }


//Client Side Function On complete of upload to check if Any error has occured
        function UploadComplete(sender, args) {
            var textError = $get('<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("Label1").ClientID %>').innerHTML;
            if (textError.length > 0) {
                $('#' + '<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("imgproj").ClientID %>', window.parent.document).attr("src", "uploads/connecting.png"); //
                $('#' + '<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("Label1").ClientID %>').fadeIn();
                $("#diverror").fadeIn(4000, function() {
                    $(this).fadeOut(6000, function() {
                        clearContents();
                    });
                });
                return;
            }
            else {
                var img = new Image();
                img.src = args.get_fileName();

                img.onload = function() {
                    $('#' + '<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("imgproj").ClientID %>').attr('src', img.src);
                }
            }
        }





C#
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
   {

       if (AsyncFileUpload1.HasFile)
       {
           System.Drawing.Image postedImage = System.Drawing.Image.FromStream(this.AsyncFileUpload1.PostedFile.InputStream);
          // if (postedImage.Width <= 100 || postedImage.Height <= 94)
           {


               //int fileSize = Int32.Parse(Path.GetFileName(e.FileSize));
               // if (fileSize < 85000) //size in bytes
               // {
               string filext = Path.GetExtension(AsyncFileUpload1.FileName);
               {

                   //string path = Server.MapPath("temp/") + Path.GetFileName(e.FileName);
                   string sessionId = this.Session.SessionID;
                   string path = Server.MapPath("temp/") + sessionId + filext;
                   AsyncFileUpload1.SaveAs(path);
                   string imgSrc = "temp/" + sessionId + filext;
                   Session["projImage"] = path;
                   ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "showImage", "showImage('" + imgSrc + "');", true);

               }
           }
           //else
           //{
           //    //string textError = "File is bigger than 100KB.";
           //    string textError = "Please Upload Image of the resolution 100x94.";

           //    ////client-side error
           //   // ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "size", "top.$get(\"" + Label1.ClientID + "\").innerHTML='" + textError + "';", true);
           //    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "showImage", "showImage('Inside File not found');", true);
           //    // ClientScript.RegisterStartupScript(this.GetType(), "js", "confirmCallBack();", true);

           //    return;
           //}
       }
       else
       {
           ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "showImage", "showImage('File not found');", true);
       }
Tags: .NET (.NET 3.5), Ajax, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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