Kentico 10 - Override File Upload Error Message

adib salhani asked on February 27, 2017 10:59

I have the following case and I really appreciate your kind support to resolve it:

  • I’ve created a new page type (let’s call it MyNewsPageType) which has been inherited from original News page type.
  • On that page type, I’ve Teaser field on which I will use it to upload an image.
  • I created a custom CMSLoaderAttribute and registered the following event:

AttachmentInfo.TYPEINFO.Events.Insert.Before += Insert_Before;

I did that because I have a custom logic I want to execute before uploading/persisting file to Kentico DB.

  • In case one of my business rule was not met, I need to terminate the upload process and display a custom error message to the user based on the error/exception occurred.

  • What I did so far, to terminate the upload process, I throw a general Exception and I pass my custom message. However, although the file not being uploaded, the error messagebeing displayed to the user is always the same (check attached image). Which means that my custom error message is being overridden somewhere in the file upload handler (CMSModules/Content/CMSPages/MultiFileUploader.ashx). The message that being returned is: “An error occurred during the file upload.”

Please let me know how I can replace that message with a custom error.

Code of CMSLoaderAttribute :


[FileUploadEventsAttribute]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Attribute class that ensures the loading of custom handlers.
    /// </summary>
    private class FileUploadEventsAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// The system executes the Init method of the CMSModuleLoader attributes when the application starts.
        /// </summary>
        public override void Init()
        {
            AttachmentInfo.TYPEINFO.Events.Insert.Before += Insert_Before;
        }

        private void Insert_After(object sender, ObjectEventArgs e)
        {
            AttachmentInfo current = (AttachmentInfo)e.Object;
        }

        private delegate void DoStuff();

        private void Insert_Before(object sender, ObjectEventArgs e)
        {
            var myValidationLogic = false; // I fixed the value just for demonstration poupose.
            if (!myValidationLogic)
            {
                var customErrorMessage = "this is the message that should be displayed to the user";
                throw new Exception(customErrorMessage);
            }
        }
    }

Recent Answers


Zoltán Jalsovszky answered on February 27, 2017 13:41 (last edited on February 27, 2017 13:41)

Hi Adib,

Unfortunately it's not possible to pass a custom message in an event handler. You will need to implement your own File uploader control, which will do the custom validation and display the message you need: https://docs.kentico.com/k10/custom-development/developing-form-controls

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.