0

Is there a way to check if the request is coming from Browse for media files

Browse for media files

Or is it coming upload file or upload files

Upload file or files

I have code to check upload image size

public class ImageSizeCheck : UploadProcessor
    {
        public List<string> RestrictedExtensions { get; set; }

        public ImageSizeCheck()
        {
            RestrictedExtensions = new List<string>();
        }

        public void Process(UploadArgs args)
        {
            Assert.ArgumentNotNull((object)args, "args");
            if (args.Destination == UploadDestination.File)
                return;

            foreach (string index in args.Files)
            {
                HttpPostedFile file = args.Files[index];
                if (!string.IsNullOrEmpty(file.FileName) && IsRestrictedExtension(file.FileName))
                {
                    if ((long)file.ContentLength > MaxImageSizeInDatabase)
                    {
                        String message = string.Format("The image {0} can not be uploaded. The maximum size should be {1}.", file.FileName, MainUtil.FormatSize(MaxImageSizeInDatabase));
                        args.ErrorText = message;
                        Log.Warn(args.ErrorText, this);
                        message = "\"" + message + "\"";
                        string str = "<script>alert(" + message + ")</script>";
                        HttpContext.Current.Response.Write(str);
                        args.AbortPipeline();
                        break;
                    }
                }
            }
        }

        private bool IsRestrictedExtension(string filename)
        {
            return RestrictedExtensions.Exists(restrictedExtension => string.Equals(restrictedExtension, Path.GetExtension(filename), StringComparison.CurrentCultureIgnoreCase));
        }

        public static long MaxImageSizeInDatabase
        {
            get
            {
                return Sitecore.Configuration.Settings.GetLongSetting("Media.MaxImageSizeInDatabase", 524288000L);
            }
        }
    }

The alert is working for UploadFile or UploadFiles but it is giving error for Browse for media files.

SyntaxError: Unexpected token < in JSON at position 0   

error

If I find that if the request is coming from Browse for media files then I can skip HttpContext.Current.Response.Write(str);

Any suggestion would be appreciated. Thanks in advance.

1 Answer 1

1

It seems to me that the message should display meaningful message rather than unexpected json token message.

Could you please perform the following steps to make it display the meaningful message:

Open the Uploader.js via <sitecore_instance>\sitecore\shell\client\Business Component Library\Layouts\Renderings\Forms\Uploader Find the following upload method:

upload: function () 
   

Replace the above upload method code with the following:

upload: function () { var that = this; this.startUploadTimer(this.datas); this.updateUploadInfo(true, true);

  //for each files - do the upload
  _.each(this.datas, function (data) {
    data.submit().error(function (jqXHR, textStatus, errorThrown) {
      if (jqXHR.status === 401) {
        _sc.Helpers.session.unauthorized();
        return;
      }
      // Triggers error on each model that had an abort
      if (jqXHR.responseText.indexOf('errorItems') != -1) {
        var errordata = jqXHR.responseText.split('</html>');
        var parsedJson = JSON.parse(errordata[1]);
        _.each(parsedJson.errorItems[1].Message, function (error) {
          error.id = data.__id;
        });
        var errors = [{ Message: parsedJson.errorItems[1].Message }];
        that.app.trigger("sc-error", errors);
        that.app.trigger("upload-error", { id: data.__id, errors: parsedJson.errorItems[1].Message });

        return undefined;
      }
      else{
        if (errorThrown === "abort") {
          that.app.trigger("upload-error", { id: data.__id });
        } else {
          var errors = [{ Message: errorThrown }];
          that.app.trigger("sc-error", errors);
          that.app.trigger("upload-error", { id: data.__id, errors: errors });
        }
      } 
    });
  });
},

After you perform the above steps, you should be able to see the meaningful message display on the upload media about why the media item is not uploaded successfully.

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.