Check filename length on the client side

7 posts, 0 answers
  1. Charlie
    Charlie avatar
    8 posts
    Member since:
    Jul 2011

    Posted 07 Jul 2011 Link to this post

    Hi,

    Is there any way to check the length of filename before uploading process? If the length is greater than 80 characters then cancel upload.
    Currently the only way to cancel upload is OnClientFilesSelected event but I can not get filename from event argument.

    Any help please?

    Thanks,
    Charlie

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 07 Jul 2011 Link to this post

    Hello Charlie,

    You can cancel upload by checking for length of file name in OnClientFileSelected event.

    Javascript:
    <script type="text/javascript">
    function OnClientFileSelected(sender,args)
    {
      if(args.get_fileName().length>80)
      {
       alert("Upload Failed");
      }
    }
    </script>

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Charlie
    Charlie avatar
    8 posts
    Member since:
    Jul 2011

    Posted 07 Jul 2011 Link to this post

    Hi Princy,

    But in OnClientFileSelected event there's not set_cancel function to be called or there's the way to do that?

    Thanks,
    Charlie
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 07 Jul 2011 Link to this post

    Hello Charlie,

    In order to cancel the event after checking for filename length, set "args.set_cancel" as true.

    Javascript:
    <script type="text/javascript">
    function OnClientFileSelected(sender,args)
    {
      if(args.get_fileName().length>80)
      {
       alert("upload failed");
       args.set_cancel(true);
      }
    }
    </script>

    Thanks,
    Princy.
  6. Charlie
    Charlie avatar
    8 posts
    Member since:
    Jul 2011

    Posted 07 Jul 2011 Link to this post

    Hi Princy,

    Thanks for your prompt reply. I tried that but got the error.
    Uncaught TypeError: Object [object Object] has no method 'set_cancel'

    It seems that the set_cancel method can only be used in OnFilesSelected event?

    Thanks,
    Charlie
  7. moegal
    moegal avatar
    273 posts
    Member since:
    Jul 2007

    Posted 19 Apr 2013 Link to this post

    is this still the case?  How do I cancel a file from OnClientFileSelected?

    I want to do all of may validation before I manually upload any of the files.

    The files can be very large and I do not want to waste the bandwidth or time uploading files that should never be uploaded.

    Thanks,  Marty
  8. Plamen
    Admin
    Plamen avatar
    2734 posts

    Posted 24 Apr 2013 Link to this post

    Hello Marty,

     
    You can achieve such size check only in browsers that use FileApi upload module (FireFox, Chrome, IE 10 etc.). Here is the code that will do this by adding a custom attribute to the row that you want to cancel:

    function OnClientFileSelected(sender, args) {
                    
                    
                    if (args.get_fileInputField().files[args.get_rowIndex()].size > 600000) {
                        args.get_row().setAttribute("stop", "stop")
                    }
                }
     
                function OnClientFileUploading(sender, args) {
                    if( args.get_row().getAttribute("stop") == "stop") {
                        args.set_cancel(true);
                    }
                }

    Unfortunately in other upload modules the size can not be found i this client event. Hope this information will be helpful.

    All the best,
    Plamen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017