Validation in custom handler

2 posts, 0 answers
  1. Joe Terhune
    Joe Terhune avatar
    11 posts
    Member since:
    Jan 2007

    Posted 23 Apr 2013 Link to this post

    I have a custom handler that I am using to encrypt and store  uploaded tiff files to a database.  I want to add validation that would check the file to see if it is corrupt before saving. How do I send an error message back to the client?
  2. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 26 Apr 2013 Link to this post

    Hi Joe,

    You could return an error message as you change the result returned by the Handler's process method
    protected override IAsyncUploadResult Process(UploadedFile file, HttpContext context, IAsyncUploadConfiguration configuration, string tempFileName)
    {
        var queryStringParam1 = context.Request.QueryString["saveToFolder"];
        string targetFolder = context.Server.MapPath(configuration.TargetFolder);
        string fileName = file.GetName();      
     
        CustomAsyncUploadResult result = CreateDefaultUploadResult<CustomAsyncUploadResult>(file);
        //file.SaveAs(targetFolder + "/" + fileName);
         
        if (true)
        {
            //Success
            result.ErrorMessage = String.Empty;
        }
        else
        {
            // Error. Set error message      
            result.ErrorMessage = "File failed to upload in DB";
        }   
        
        return result;
    }

    and then handle it on the client in OnClientFileUploaded event:
    function OnClientFileUploaded(sender, args) {
        var msg = args.get_fileInfo().ErrorMessage;
     
        if (msg) {
            //Display error message
            $('#msg').text(msg);
            //Remove selected file
            $('.ruButton.ruRemove').click();
        }
    }

    For your reference I'm attaching small sample demonstrating how this code works. Additionally you could take a look at this online demo which implements the same approach.

    Kind regards,
    Hristo Valyavicharski
    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
Back to Top