Sitecore Pipeline Upload Processor

2019-07-21 00:18发布

I'm using UploadProcessor to block specific file uploading into MediaLibrary. Everything is working good and I can see the alert Sitecore's message. But, Sitecore's error message is not really user-friendly "One or more files could not be uploaded. See the Log file for more details"

So, I'd like to add extra alert box for users. Below is my code, but the javascript is not working.

enter image description here

Some people want me to use "SheerResponse", but Sitecore Document mentions that

The uiUpload pipeline is run not as part of the Sheer event, but as part of the form loading process in response to a post back. This is because the uploaded files are only available during the “real” post back, and not during a Sheer UI event. In this sense, the uiUpload pipeline has not been designed to provide UI. In order to provide feedback to a User, the processor should resort to some trick which emits the JScript code. http://sdn.sitecore.net/Articles/Media/Prevent%20Files%20from%20Uploading/Pipeline%20upload.aspx

Do you have any idea how to implement alert box??

1条回答
Viruses.
2楼-- · 2019-07-21 01:08

The Upload control in the Media Library uses flash to upload the files. As part of this upload process, the file sizes are checked using JavaScript and a client side validation is made before the upload.

There are a number of changes you need to make. I'm just going to list them here, you can find all the code in my Github Gists:

https://gist.github.com/jammykam/54d6af46593fa3b827b4


1) Extend and update the MediaFolder.js file to check the file size against the Image Size ONLY if the extension is one specified in config

if (file.size > this.uploadLimit() || this.uploadImageLimitReached(file)) {
    ...
}

2) Update MediaFolder.xml page to include the above JS. Amend the codebeside, inheriting from Sitecore.Shell.Applications.Media.MediaFolder.MediaFolderForm and overriding OnLoad and OnFilesCancelled, to render restricted extensions and max image size settings so these are passed to the Javascript and display friendly alert.

settings.Add("uploadImageLimit", ((long)System.Math.Min(ImageSettings.MaxImageSizeInDatabase, Settings.Runtime.EffectiveMaxRequestLengthBytes)).ToString());
settings.Add("uploadImageRestrictedExtensions", ImageSettings.RestrictedImageExtensions);

3) Update Attach.xaml.xml codebeside to check image size, inheriting from Sitecore.Shell.Applications.FlashUpload.Attach.AttachPage and overriding OnQueued method:

if (ImageSettings.IsRestrictedExtension(filename) && num > maximumImageUploadSize)
{
    string text = Translate.Text("The image \"{0}\" is too big to be uploaded.\n\nThe maximum image size that can be uploaded is {1}.", new object[] { filename, MainUtil.FormatSize(maximumImageUploadSize) });
    this.WarningMessage = text;
    SheerResponse.Alert(text, new string[0]);
}
else
{
    base.OnQueued(filename, lengthString);
}

4) Add a config include with the new settings.

<setting name="Media.MaxImageSizeInDatabase" value="1MB" />
<setting name="Media.RestrictedImageExtensions" value=".jpg|.jpeg|.png|.gif|.bmp|.tiff" />

You can still (and should) keep the pipelines in place, but note from my previous answer I gave the "Restricted Extension" config setting has now changed (into a single setting instead of passing it into the pipeline). The Gist contains the

NOTE that I have tested this using Sitecore 7.2 rev 140526, so the base code is taken from there. If you are using a different version then you should check the base C#, JS and XML code matches what I have provided. The code is commented to show you what has changed.

The above works in the Content Editor, it does not work in the Page Editor! Which in Sitecore 7.2+ uses SPEAK dialogs and it looks like they use a different set of pipelines. That would need more investigation (raise another question, and specify which version of Sitecore you are using).

查看更多
登录 后发表回答