Custom Validation on FileSelected

5 posts, 0 answers
  1. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 20 Nov 2011 Link to this post

    This sounded like such a simple request but I simply can't figure it out.

    I want to prevent the user from selecting certain file types (I'm trying to implement a blacklist type of validation rather than the whitelist method that the telerik control comes with out-of-the-box).

    OK, so there's no set_cancel() method for OnClientFileSelected. A shame, but moving on.

    I though, OK, if they select an invalid file type, I'll just remove it, so came up with this ....
    var fileExtensionBlacklist = "<%=DisallowedFileTypes %>";
    function OnClientFileSelected(sender, e)
    {
        if("<%=UseBlacklist %>" == "True")
        {
            var selectedFileName = e.get_fileName();
            var selectedExtension = selectedFileName.split(".").pop();
            if (fileExtensionBlacklist != "" && fileExtensionBlacklist.indexOf(selectedExtension) > -1)
            {
                try { $find("<%=RadAsyncUpload1.ClientID %>").deleteFileInputAt(e.get_rowIndex()); } catch (ex) { }
            }
        }
    }
    function OnClientFileUploadFailed(sender, e)
    {
        e.set_handled(true);
    }
    This doesn't work, the attempt to delete the file input generates an error reporting "HtmlPage_NotEnabled" in a line of javascript that calls Content.Page.MarshalUploads(). This appears to be something to do with Silverlight, but I can't stop it from happening.

    Working to a deadline, I moved on.

    So, I thought, "OK, if I can't stop 'em selecting these files, I can at least, stop 'em from being uploaded".

    So, I modified the code to this ...
    function OnClientFileUploading(sender, e)
    {
        if("<%=UseBlacklist %>" == "True")
        {
            var selectedFileName = e.get_fileName();
            var selectedExtension = selectedFileName.split(".").pop();
            if (fileExtensionBlacklist != "" && fileExtensionBlacklist.indexOf(selectedExtension) > -1)
            {
                e.set_cancel(true);
            }
        }
    }
    Tried that and Yay! the file is mared red as a failed upload!

    Then I click on the button on my page to actually do the postback and process the uploaded files. Get what I find in my server-side list of uploaded files, yep. The file that I had set cancel on. What's more it shows it IsValid as true.

    Is this God's punishment for me working on a Sunday? If it is, he's picking on the wrong bloke, I have no choice; he should go smite the guy who said I had to.

    If anyone can offer any suggestions as to what I'm doing wrong, I'd love to hear 'em.

    --
    Stuart


  2. Peter Filipov
    Admin
    Peter Filipov avatar
    1028 posts

    Posted 23 Nov 2011 Link to this post

    Hi Stuart,

    Your approach to use the OnClientFileUploading event is correct. The issue is that the canceled file is uploaded. It happens only when the Silverlight module is used.
    You can disable it with the following sample code (right before the declaration of the RadAsyncUpload control) or by setting the DisablePlugins property to true.
    E.g. :
    Telerik.Web.UI.RadAsyncUpload.Modules.Silverlight.isAvailable = function() { return false; };
    I logged the problem and it is going to be fixed.

    All the best,
    Peter Filipov
    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 25 Nov 2011 Link to this post

    Peter,

    thanks for the info about the Silverlight bug being fixed.

    Can I put in a request for a OnClientFileSelecting() method that's cancelable?

    --
    Stuart
  5. Peter Filipov
    Admin
    Peter Filipov avatar
    1028 posts

    Posted 29 Nov 2011 Link to this post

    Hi Stuart,

    We are going to investigate the case with Silverlight module and canceling the upload. If we add OnClientFileSelecting event, it will be used like OnClientFileUploading event.

    Kind regards,
    Peter Filipov
    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
  6. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 30 Nov 2011 Link to this post

    Cool.

    -- 
    Stuart
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017