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

Custom Validation on FileSelected

4 Answers 108 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Stuart Hemming
Top achievements
Rank 2
Stuart Hemming asked on 20 Nov 2011, 12:43 PM
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


4 Answers, 1 is accepted

Sort by
0
Peter Filipov
Telerik team
answered on 23 Nov 2011, 12:56 PM
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
0
Stuart Hemming
Top achievements
Rank 2
answered on 25 Nov 2011, 12:25 PM
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
0
Peter Filipov
Telerik team
answered on 29 Nov 2011, 05:58 PM
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
0
Stuart Hemming
Top achievements
Rank 2
answered on 30 Nov 2011, 09:34 AM
Cool.

-- 
Stuart
Tags
AsyncUpload
Asked by
Stuart Hemming
Top achievements
Rank 2
Answers by
Peter Filipov
Telerik team
Stuart Hemming
Top achievements
Rank 2
Share this question
or