Validating a file upload with the RadCaptcha

Thread is closed for posting
1 posts, 0 answers
  1. 63F75A2C-1F16-4AED-AFE8-B1BBD57646AD
    63F75A2C-1F16-4AED-AFE8-B1BBD57646AD avatar
    1572 posts
    Member since:
    Oct 2004

    Posted 14 Mar 2012 Link to this post


    RadControls version 2012.1.215
    .NET version 3.x/4.x
    Visual Studio version 2008/2010
    programming language C# and VB.NET
    browser support

    all browsers supported by RadControls

    Validating a RadCaptcha requires a postback to the server, because this validation can only be performed in the code-behind (which is the purpose of the RadCaptcha). In a normal case, however, a postback will remove the file the user has selected, as the upload control's client state will be cleared. This means that either validation must be performed in two steps where the first is validating, the second is the actual upload, or the user must select the file each time an invalid code is entered. Both of these cases are not desired by the end user, because they require extra effort.

    There is a way to get around this by using the RadControls for ASP.NET AJAX - the RadAsyncUpload offers the functionality to persist its client state across postbacks and only upload the files in case a specific button is clicked. This is the PostbackTriggers property. More information about it is available in this help article and this online demo. The difficulty is using the same button for validating the RadCaptcha and uploading the file - if this file is configured as a postback trigger and an invalid code is entered the UploadedFiles collection will be lost, if it is not this collection will not be available. The solution for this is to store the old captcha text on the server (for example in the Session) and validate it manually in the PageInit event so you can modify the PostbackTriggers property accordingly - if the old value and the value the user posted match - set the postback trigger to the submit button so that we can later access the UploadedFiles, otherwise - keep it pointing to a hidden button whose purpose is to persist the client state. The file manipulation takes place in the button click event - a regular check for the captcha validation and a simple method to store the file.

    The sample code with additional comments that explain each line is available in the attached file.

    For convenience the same post exists in the RadAsyncUpload section here.
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.