locked
AjaxFileUpload "OnClientUploadError" event not firing RRS feed

  • Question

  • User1849990362 posted

    Hi,

    I have used "AjaxFileUpload" control.

    <asp:AjaxFileUpload ID="AjaxFileUpload" MaximumNumberOfFiles="1" ContextKeys="KeyHiddenField" OnUploadComplete="AjaxFileUpload_OnUploadComplete"
            OnClientUploadComplete="OnClientUploadComplete" OnClientUploadError="OnClientUploadErrorTest" runat="server" />
    

    In code behind

    protected void AjaxFileUpload_OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {
    // want to fire "OnClientUploadError" javascript event here explicitely.
     throw new NullReferenceException();
    }
    

    Javascript :

    function OnClientUploadErrorTest(sender, args) {
      
    }
    

    OnClientUploadComplete is firing properly.

    For testing i want to fire OnClientUploadError event in JavaScript for that I have explicitly thrown exception in code behind.
    But it’s not firing at all.







    Tuesday, October 23, 2012 3:07 AM

Answers

  • User-650628323 posted

    Hi,

    AjaxFileUpload "OnClientUploadError" event not firing

    In order to resolve  your issue, please see the demo below:

    In the .aspx

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function OnClientUploadComplete() {
                alert("upload complete");
            }
            function showUploadError(sender, args) {          
              alert(args.get_errorMessage()); 
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
            </asp:ToolkitScriptManager>
            <asp:AjaxFileUpload ID="AjaxFileUpload1"  AllowedFileTypes="jpg,jpeg"
                runat="server" OnUploadComplete="AjaxFileUpload1_UploadComplete" OnClientUploadError="showUploadError"
                OnClientUploadComplete="OnClientUploadComplete" />
        </div>
        </form>
    </body>
    </html>
    

    In the .cs

      protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            string filePath = Request.PhysicalApplicationPath + "UploadedImages\\test.jpg";
            AjaxFileUpload1.SaveAs(filePath);
        }
    

    For more information about AjaxFileUpload, please check:

    http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 30, 2012 12:31 AM

All replies

  • User555306248 posted

    Refer this - http://forums.asp.net/t/1620604.aspx/1

    Wednesday, October 24, 2012 11:12 PM
  • User555306248 posted

    You can use OnClientUploadError  The name of a JavaScript function executed in the client-side if the file upload failed.

    http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx

    Wednesday, October 24, 2012 11:15 PM
  • User1849990362 posted

    "OnClientUploadError " is not getting called even if in "UploadedComplete" event an exception is thrown.

    In what scenario OnClientUploadError will get called?

    Thursday, October 25, 2012 4:28 AM
  • User-650628323 posted

    Hi,

    AjaxFileUpload "OnClientUploadError" event not firing

    In order to resolve  your issue, please see the demo below:

    In the .aspx

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function OnClientUploadComplete() {
                alert("upload complete");
            }
            function showUploadError(sender, args) {          
              alert(args.get_errorMessage()); 
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
            </asp:ToolkitScriptManager>
            <asp:AjaxFileUpload ID="AjaxFileUpload1"  AllowedFileTypes="jpg,jpeg"
                runat="server" OnUploadComplete="AjaxFileUpload1_UploadComplete" OnClientUploadError="showUploadError"
                OnClientUploadComplete="OnClientUploadComplete" />
        </div>
        </form>
    </body>
    </html>
    

    In the .cs

      protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            string filePath = Request.PhysicalApplicationPath + "UploadedImages\\test.jpg";
            AjaxFileUpload1.SaveAs(filePath);
        }
    

    For more information about AjaxFileUpload, please check:

    http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 30, 2012 12:31 AM
  • User947405739 posted

    Hi.

    With asp:AjaxFileUpload the OnClientUploadError not fired. Test this example:

    The code validate the size of file onClient. When Size > 1000000 fire exception. The exception not fired OnClientUploadError="uploadError" and the file was upload to the server.

    I need to validate de size of file onCliente to prevent the fileupload on the server.

    <script type="text/javascript">
    
    
        function uploadError(sender, args) {
            //Good practice to put try,catch block. it will avoid javascript error at window status.
                var ArchivoNombre = args.get_fileName();
                var MensajeError = args.get_errorMessage();
                var Mensaje = "Archivo no cargado: " + ArchivoNombre + " / ERROR..." + MensajeError;
    
                //Desarrollar algún proceso en el servidor: grabar en base de datos, etc.
                //  En este caso, se crea una variable de sesión. 
                //PageMethods.RegisterUploadError(MensajeError, Mensaje, OnSucceeded, OnFailed);
            }
        }
    
        function uploadComplete(sender, e) {
    
            var Peso = context.get_fileSize();
            var ArchivoNombre = context.get_fileName();
    
            /*Validation for file size*/
            if (parseInt(Peso) > 1000000) {
    
                var Mensaje = "Archivo excede el tamaño límite de 2 Mb. Archivo : " + ArchivoNombre + " Peso(bytes): " + Peso;
                var displayElement = document.getElementById("ResultId");
                displayElement.innerHTML = Mensaje;
    
                var err = new Error();
                err.name = 'Error Uploading';
                err.message = 'Excedió el límite.';
                throw (err);
    
            }
            else {
    
              //Etc...            
    
    
            }
        }
    </script>
    
    

    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
            </asp:ToolkitScriptManager>
            
            <asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" 
            OnClientUploadComplete="uploadComplete" 
            OnClientUploadError="uploadError" 
            onuploadcomplete="AjaxFileUpload1_UploadComplete" />
    
            <div id="testuploaded" style="display: none; padding: 4px; border: gray 1px solid;">
                <h4>
                    list of uploaded files:</h4>
                <hr />
                <div id="fileList">
                </div>
            </div>
    
        <p>
            <span style="background-color:Aqua" id="ResultId"></span>
        </p>
    
            <asp:Button ID="btnSubmit" runat="server" Text="Postback" />
    
        </div>
        </form>
    </body>

    CodeBehind:

        protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs file)
        {
            //Grabar en disco
            string rutaUploads = HttpContext.Current.Server.MapPath("Uploads\\");
            string FullNombre = rutaUploads + file.FileName;
            AjaxFileUpload1.SaveAs(FullNombre);
    
        }
    

    Monday, December 16, 2013 10:32 AM