Search code examples
asp.netasyncfileupload

ASP.NET AsyncFileUpload RegisterClientScriptBlock not working


I have a problem working with ScriptManager's RegisterClientScriptBlock which is not working.

I have a AsyncFileUpload and I want to preview the uploaded image dynamically after the upload.

the UploadedComplete is wired in Page_Init and here's the UloadedComplete block code in server side

void fileUpload_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    string csb = string.Empty; 
    string tempfolder = "picserver/posts/temp/" + Session["Username"] + "_" + DateTime.Today.ToString("yyyy_MM_d") + ".jpg";
    string newfile = Server.MapPath(tempfolder);

    fileUpload.SaveAs(newfile);

    Byte[] f = File.ReadAllBytes(newfile);
    Imaging i = new Imaging();
    i.ResizeImageFile(newfile, newfile, 64);

    csb = "$(\"" + postImagePreview.ClientID + "\").innerHTML = '<img src=\"" + tempfolder + "\" alt=\"\" />'";
    System.Diagnostics.Debug.WriteLine("CSB: " + csb);
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "preview", csb, true);
}

and here's the html

<asp:Label ID="postImagePreview" runat="server" />
Upload a preview of your advertisement.
<asp:AsyncFileUpload ID="fileUpload" runat="server" UploaderStyle="Traditional" UploadingBackColor="#CCFFFF" ThrobberID="myThrobber" /> <asp:Label runat="server" ID="myThrobber" style="display:none;" ></asp:Label>

here's my ScriptBlock

csb = "$(\"" + postImagePreview.ClientID + "\").innerHTML = '<img src=\"" + tempfolder + "\" alt=\"\" />'";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "preview", csb, true);

and it's not working.. even if I put "alert('hello');" in script parameter. still doesn't work.

you guys have any idea why?


Solution

  • This is caused by a bug in the current version of the AsyncFileUpload control. Essentially it clears the content, writes the control, and then ends the request. This prevents the script block from rendering. More details and a workaround can be seen on the AJAX Control Toolkit bug tracker