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

Validation in custom handler

1 Answer 47 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Joe Terhune
Top achievements
Rank 2
Joe Terhune asked on 23 Apr 2013, 05:45 PM
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?

1 Answer, 1 is accepted

Sort by
0
Hristo Valyavicharski
Telerik team
answered on 26 Apr 2013, 02:52 PM
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.
Tags
AsyncUpload
Asked by
Joe Terhune
Top achievements
Rank 2
Answers by
Hristo Valyavicharski
Telerik team
Share this question
or