Display Exception Message from Custom HTTP Handler

6 posts, 1 answers
  1. Gary
    Gary avatar
    20 posts
    Member since:
    Oct 2012

    Posted 31 Dec 2013 Link to this post

    I have made my own custom HTTP handler to run some server side validation on files before the upload completes. I have 2 questions:

    1. If the file does not pass validation, I would like to display an error on the upload page under the uploaded file. How can I do that?
    2. If the file does not pass validation, I would like to delete it (or not have it save to the disk at all in the first place). Is there a way to do that?

    Thanks.
  2. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 02 Jan 2014 Link to this post

    Hi Gary,

    You may create result, which will be return on the client after the file is send to handler then check this custom result on the client and remove the row:

    CustomResult.cs
    public class CustomResult : AsyncUploadResult
    {
        public int StatusCode { get; set; }
        public string Message { get; set; }
    }

    Handler.ashx
    public class Handler : AsyncUploadHandler, System.Web.SessionState.IRequiresSessionState
    {
     
        protected override IAsyncUploadResult Process(UploadedFile file, HttpContext context, IAsyncUploadConfiguration configuration, string tempFileName)
        {
            CustomResult result = CreateDefaultUploadResult<CustomResult>(file);
             
             
            string targetFolder = context.Server.MapPath("~/Uploads/");
            string fileName = file.GetName();
     
            if (file.GetExtension() == ".jpg")
            {
                file.SaveAs(targetFolder + fileName);
                result.StatusCode = 201;
                result.Message = "File was uploaded successfully.";
            }
            else
            {
                result.StatusCode = 415;
                result.Message = "Error! File extension is not allowed";
            }
     
            return result;
        }
    }

    ASPX:
    function OnClientFileUploaded(sender, args) {
        //check what is the result returned by the handler
        if (args.get_fileInfo().StatusCode == 415) {
            //remove the selected file
            args.get_row().remove();
            //display a notification
            alert(args.get_fileInfo().Message);
        }
    }

    Sample is attached.

    Regards,
    Hristo Valyavicharski
    Telerik
    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 the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Gary
    Gary avatar
    20 posts
    Member since:
    Oct 2012

    Posted 02 Jan 2014 Link to this post

    Thank you Hristo. That seems to work well.

    But in my case, I am only allowing 1 file upload and when I remove the row with "args.get_row().remove()", the uploaded file listing gets removed, but the select box does not reappear so the user can't select another file. If I comment out the line that removes the file, then the file is there with a little green circle next to it that seems to show that the file was uploaded successfully - even though an error message shows up..

    What I would really like, is if the file would remain listed, but with a little red circle next to it - the same thing that happens when someone selects a file with an extension that is not in the allowed list. How can I do that?
  5. Gary
    Gary avatar
    20 posts
    Member since:
    Oct 2012

    Posted 02 Jan 2014 Link to this post

    I also see that even if the file fails validation in the HTTP handler, it still gets saved to the "~/AppData/RadUploadTemp" folder. When it fails, I don't want it to hit my hard drive at all. How can I stop this from happening?
  6. Answer
    Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 03 Jan 2014 Link to this post

    Hi Gary,

    If you need to make validation of the file type or size better use AllowedFileExtensions and MaxFileSize properties of the AsyncUpload instead validate them into the handler.
    <telerik:RadAsyncUpload
        ID="RadAsyncUpload1"
        OnClientFileUploaded="OnClientFileUploaded"
        HttpHandlerUrl="~/Handler.ashx"
        AllowedFileExtensions="jpg"
        MaxFileInputsCount="1"
        MaxFileSize="99999"
        TargetFolder="~/Uploads"
        runat="server">
    </telerik:RadAsyncUpload>

    After you return the result from the handler you will have to set the red circle manually: 
    function OnClientFileUploaded(sender, args) {
        if (args.get_fileInfo().StatusCode == 415) {
            setTimeout(function () {
                $(args.get_row()).find('span .ruUploadProgress').removeClass('ruUploadFailure');
                $(args.get_row()).find('span .ruUploadProgress').addClass('ruUploadFailure');
            }, 100);
        }
    }

    Regarding your other question. All files are stored first into the temporary folder. Some of them will be uploaded successfully and may be moved automatically to the Target folder or after you call File.SaveAs() method. However all files that remains into the temporary folder will be cleared automatically by the RadAsyncUpload on every 4 hours. This can be changed as you set the TemporaryFileExpiration property.

    Regards,
    Hristo Valyavicharski
    Telerik
    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 the blog feed now.
  7. Gary
    Gary avatar
    20 posts
    Member since:
    Oct 2012

    Posted 03 Jan 2014 Link to this post

    I added that JS code and it works perfectly.

    I am using the allowed extensions option. But I also check server side with other checks that ignore the extension in case a malicious user has spoofed the file extension. 

    Thanks for your help!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017