Sitecore 管道上传处理器

Sitecore Pipeline Upload Processor

我正在使用 UploadProcessor 来阻止将特定文件上传到 MediaLibrary。 一切正常,我可以看到 Sitecore 的警报消息。但是,Sitecore 的错误信息并不是 user-friendly "One or more files could not be uploaded. See the Log file for more details"

所以,我想为用户添加额外的警告框。下面是我的代码,但是 javascript 不工作。

有些人希望我使用 "SheerResponse",但 Sitecore 文档提到

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

你知道如何实现警告框吗?

媒体库中的上传控件使用 flash 上传文件。作为此上传过程的一部分,使用 JavaScript 检查文件大小,并在上传前进行客户端验证。

您需要进行一些更改。我只是在这里列出它们,您可以在我的 Github Gists 中找到所有代码:

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


1) 扩展和更新 MediaFolder.js 文件以根据图像大小检查文件大小,仅当扩展名是配置

中指定的扩展名时
if (file.size > this.uploadLimit() || this.uploadImageLimitReached(file)) {
    ...
}

2) 更新 MediaFolder.xml 页面以包含上述 JS。修改 codebeside,继承自 Sitecore.Shell.Applications.Media.MediaFolder.MediaFolderForm 并覆盖 OnLoadOnFilesCancelled,以呈现受限制的扩展和最大图像大小设置,以便将这些设置传递给 Javascript 并显示友好的警报。

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

3) 更新 Attach.xaml.xml codebeside 以检查图像大小,继承自 Sitecore.Shell.Applications.FlashUpload.Attach.AttachPage 并覆盖 OnQueued 方法:

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) 添加包含新设置的配置。

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

您仍然可以(并且应该)将管道保持在原位,但是我的 请注意,我给的 "Restricted Extension" 配置设置现在已更改(变成一个设置而不是将其传递到管道)。要点包含

请注意,我已经使用 Sitecore 7.2 版本 140526 对此进行了测试,因此基本代码取自那里。如果您使用不同的版本,那么您应该检查基本的 C#、JS 和 XML 代码是否与我提供的相匹配。注释代码以向您显示更改内容。

以上内容在内容编辑器中有效,在页面编辑器中无效!在 Sitecore 7.2+ 中使用 SPEAK 对话框,看起来它们使用了一组不同的管道。这需要更多调查(提出另一个问题,并指定您使用的是哪个版本的 Sitecore)。