This is a migrated thread and some comments may be shown as answers.

Check filename length on the client side

6 Answers 1258 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Charlie
Top achievements
Rank 1
Charlie asked on 07 Jul 2011, 10:27 AM
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

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 07 Jul 2011, 11:19 AM
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.
0
Charlie
Top achievements
Rank 1
answered on 07 Jul 2011, 11:22 AM
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
0
Princy
Top achievements
Rank 2
answered on 07 Jul 2011, 11:38 AM
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.
0
Charlie
Top achievements
Rank 1
answered on 07 Jul 2011, 11:44 AM
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
0
moegal
Top achievements
Rank 1
answered on 19 Apr 2013, 08:51 PM
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
0
Plamen
Telerik team
answered on 24 Apr 2013, 11:32 AM
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.
Tags
AsyncUpload
Asked by
Charlie
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Charlie
Top achievements
Rank 1
moegal
Top achievements
Rank 1
Plamen
Telerik team
Share this question
or