1

I am trying to validate media item whilst uploading to check if the media of certain type has a specific size limit. Created a pipeline which replaces the Sitecore.Pipelines.Upload.CheckSize to the custom pipeline.

If file is not valid i.e. within the Size limit args error text is set as below-

args.UiResponseHandler.FileTooBig(StringUtil.EscapeJavascriptString(fileName));
args.ErrorText = $"The  file {fileName} is too big to be uploaded. Testing testing The maximum size for uploading files is {limit} MB.";
args.AbortPipeline();

When I try uploading multiple files I am able to see the above custom message, but whilst uploading a single file I see the message which is set in Sitecore.Pipelines.Upload.CheckSize class i.e. instead of custom message I see this-

enter image description here

the same file uploaded using (advanced) option

enter image description here

Has anybody faced this issue, help here is appreciated.

1
  • Can you share your code for us to be able to provide a solution Commented Dec 14, 2017 at 17:32

3 Answers 3

4

The Upload file and Upload files (advanced) use different methods for communication between the frontend and backend for the alerts that are shown.

The advanced upload will display whatever you set in args.ErrorText but Upload files through a slightly roundabout way calls ShowFileTooBig(string filename) in Sitecore.Shell.Applications.Media.UploadMedia.UploadMediaForm.

Provide a new implmentation of the ShowFileTooBig method and add in the same validation as you do in your pipeline and show the correct message:

public class UploadMediaForm : Sitecore.Shell.Applications.Media.UploadMedia.UploadMediaForm
{
  private string ErrorMessage  = "The image \"{0}\" is too big to be uploaded. The maximum size for uploading images is {1}.";

  protected new void ShowFileTooBig(string filename)
  {
    string extension = Path.GetExtension(filename);
    if (!extension.Equals(".jpg"))
    {
      base.ShowFileTooBig(filename);
      return;
    }

    SheerResponse.Alert(Translate.Text(ErrorMessage, filename, MainUtil.FormatSize(ImageSettings.MaxImageSizeInDatabase)));
    this.OK.Disabled = true;
    this.Cancel.Disabled = true;
    this.OK.Disabled = false;
    this.Cancel.Disabled = false;
  }
}

Create a copy of /sitecore/shell/Applications/Media/Upload Media/UploadMedia.xml and place it in /sitecore/shell/Override. Edit the file and update the type specified in the CodeBeside tag to your own implementation. We need to provide a new implementation of the ShowFileTooBig method.

I've made an assumption that you have a custom uiUpload pipeline processor that checks the file being uploaded and returns the error. You can find a Github Gist with a fuller code example but the above should answer your specific question.

Note that your question only deals with the simple HTML version of the uploader. You also need to deal with the Flash enabled uploader. This requires further updates similar to this StackOverflow answer I previosuly provided.

1
0

I believe you may need to override parts of Sitecore.Shell.Applications.Media.UploadMedia.UploadMediaForm, Sitecore.Client and customize UploadMedia.xml. There is a check for the OnLoad method of UploadMediaPage2 which does a response write that ends up calling the method ShowFileTooBig. This will allow you to show your custom message but unfortunately I am not sure if the file will still be blocked from being uploaded if it qualified as too big for the check in ClientPage. Based on what I'm seeing I think it won't upload the file so if your desire is just to inject a custom message going this route should be fine. If you actually want to change what the ClientPage base class marks as a request that is too big then something else will be needed.

1
  • 1
    Thank Teeknow for pointing to the right direction on the issue. Commented Dec 19, 2017 at 13:42
0

Please try the below answer. It will work on both Upload File and Upload Files(Advance)

 public void Process(UploadArgs args)
    {
        Assert.ArgumentNotNull((object)args, "args");
        if (args.Destination == UploadDestination.File)
            return;
        foreach (string index in args.Files)
        {
        // If you are uploading zip file then you have to write your own logic get the file details form the zip.
            HttpPostedFile file = args.Files[index];
            if (!string.IsNullOrEmpty(file.FileName) && IsRestrictedExtension(file.FileName))
            {
                if (((long)file.ContentLength / 1024f) / 1024 > 2)//checking file size as 2MB or not
                {
                    HttpContext.Current.Response.Write("<html><head><script type=\"text/JavaScript\" language=\"javascript\">alert('Its worked');</script></head><body>Done</body></html>");
                    args.AbortPipeline();
                    break;
                }
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.