1

This is what I'm trying to do. When an image is uploaded using Ajax AsyncFilepload, it also shows the image in the Image Control. Since, the upload is asynchronous, though the image uploads fine, it doesnt show in the image Control. So, following a suggestion, added the JS function. It wrks fine now, but just that the C# code fires twice, because of the postback and hence every Image is uploaded twice.

Also, why is the ViewState becoming null, the second time. This page uses a master.

JS :

function UpdateImage()
{
  __doPostBack("UpdatePanel2","");
}

ASPX :

<asp:AsyncFileUpload ID="fu" runat="server" 
 OnClientUploadComplete="UpdateImage" OnUploadedComplete="fu_UploadedComplete"/>

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
 <ContentTemplate>
  <asp:Image runat="server" ID="img"/>
 </ContentTemplate>
</asp:UpdatePanel>

C# :

protected void fu_UploadedComplete(object sender, 
                                 AjaxControlToolKit.AsyncFileUploadEventArgs e)
{
   if(fu.HasFile && ViewState["vMap"] == null)
    {
      string sNew = getRandomNumber().ToString() + fu.FileName;
      ViewState["vMap"] = sNew;
      sNew = "~/Temp/"+sNew;
      fu.SaveAs(Server.MapPath(sNew));
      img.ImageUrl = sNew;
      UpdatePanel.Update();
    }
}
Ruby
  • 949
  • 7
  • 31
  • 68
  • The compiler is not calling your function twice - your execution plan does = the logic build by yourself! Set a breakpoint in your C# code in `fu_UploadedComplete` and once you are inside it (on every stop) have a look at the [call stack window](http://msdn.microsoft.com/en-us/library/a3694ts5.aspx) in order to see who is calling the function. – keenthinker Mar 16 '14 at 12:12
  • I am unable to find anything here..The name says 'fu_UploadedComplete(object sender, AjaxControlToolKit.AsyncFileUploadEventArgs e)'. Moreover, why has the ViewState["vMap"] become null, the second time, it visits the event. – Ruby Mar 16 '14 at 12:23
  • @Ruby Take a look to the following example may it help. http://www.asp.net/AJAXlibrary/HOW%20TO%20Use%20the%20AsyncFileUpload%20Control.ashx – alnaji Mar 16 '14 at 12:25
  • @alnaji.Could you plz tell me, how is this related to my question. I couldnt find a way to stop the second run. – Ruby Mar 16 '14 at 12:31
  • I tried all the 3 solutions from http://stackoverflow.com/questions/5018044/asyncfileupload-postback-causes-double-upload – Ruby Mar 16 '14 at 13:12

1 Answers1

0

HiE frnd...!!!!!!!
ID of your file upload control is "fu" and you are using "fileUpload.HasFile" which is wrong.
therefor output is not correct. I have some code try out this one.
*HEAD TAG*

    <script type="text/javascript">
            // This function will execute after file uploaded successfully
            function uploadComplete()
             {
                document.getElementById('<%=lblMsg.ClientID %>').innerHTML = "File Uploaded Successfully";
            }
            // This function will execute if file upload fails
            function uploadError() {
                document.getElementById('<%=lblMsg.ClientID %>').innerHTML = "File upload Failed.";
            }
        </script>

BODY

<body>
    <form id="form1" runat="server">
    <ajax:ToolkitScriptManager ID="scriptManager1" runat="server" />
    <div>
        <ajax:AsyncFileUpload ID="fileUpload1" OnClientUploadComplete="uploadComplete" OnClientUploadError="uploadError"
            CompleteBackColor="White" Width="350px" runat="server" UploaderStyle="Modern"
            UploadingBackColor="#CCFFFF" ThrobberID="imgLoad" OnUploadedComplete="fileUploadComplete" />
        <br />
        <asp:Image ID="imgLoad" runat="server" ImageUrl="loading.gif" />
        <br />
        <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>

CODE BEHIND FILE

protected void fileUploadComplete(object sender, AsyncFileUploadEventArgs e)
    {
        string filename = System.IO.Path.GetFileName(fileUpload1.FileName);
        fileUpload1.SaveAs(Server.MapPath("Files/") + filename);
    }
Hardik Parmar
  • 1,053
  • 3
  • 15
  • 39
  • Please reply if this answer was useful. If not please tell I willtry out with some other code. – Hardik Parmar Mar 16 '14 at 14:26
  • Sorry, that wasnt helpful. I do not want to show a status message. What I need to do is find a way to stop firing the C# code the second time. – Ruby Mar 17 '14 at 09:05