When we upload the media in the Sitecore media library then it happens that sometimes an issue occurs but the error message is not that friendly which can help an author/developer to know what the exact issue is.
The same situation once was in Nehemiah Jeyakumar so I took help from his blog https://www.nehemiahj.com/2022/07/sitecore-media-upload-dialog-duplicate.html to implement the functionality for our project as well.
Here we created our own version of the UploadMedia2.aspx that is responsible for showing the dialogue on uploading the media in the media library.
The UploadMedia2.aspx lives in folder C:\inetpub\wwwroot\yourProject\sitecore\shell\Applications\Media\Upload Media
Below is the code that we wrote for this aspx
<%@ Page language="c#" Codebehind="UploadMedia2.aspx.cs" AutoEventWireup="false" Inherits="MyProject.Feature.Media.sitecore.shell.Applications.Media.UploadMedia.UploadMediaPage2" %>
<%@ OutputCache Location="None" VaryByParam="none" %>
and code behind is:
using Sitecore;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Diagnostics;
using Sitecore.Exceptions;
using Sitecore.Globalization;
using Sitecore.Pipelines;
using Sitecore.Pipelines.Upload;
using Sitecore.Shell.Web.UI;
using Sitecore.Web.UI.XmlControls;
using System;
using System.Web;
using System.Web.UI;
namespace MyProject.Feature.Media.sitecore.shell.Applications.Media.UploadMedia
{
public class UploadMediaPage2 : SecurePage
{
protected override void OnInit(EventArgs e)
{
Control control = ControlFactory.GetControl("UploadMedia");
if (control != null)
{
Controls.Add(control);
}
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (base.MaxRequestLengthExceeded)
{
Log.Error($"Uploaded file is too big. Stack Trace: failed on if condition of RSM.Feature.Media.sitecore.shell.Applications.Media.UploadMedia.UploadMediaPage2", this);
HttpContext.Current.Response.Write("<html><head><script type=\"text/JavaScript\" language=\"javascript\">window.top.scForm.getTopModalDialog().frames[0].scForm.postRequest(\"\", \"\", \"\", 'ShowFileTooBig()')</script></head><body>Done</body></html>");
}
else
{
if (base.IsEvent || base.Request.Files.Count <= 0)
{
return;
}
try
{
string empty = string.Empty;
Language contentLanguage = Sitecore.Context.ContentLanguage;
string text = Sitecore.Context.ClientPage.ClientRequest.Form["ItemUri"];
ItemUri itemUri = ItemUri.Parse(text);
if (itemUri != null)
{
empty = itemUri.GetPathOrId();
contentLanguage = itemUri.Language;
UploadArgs uploadArgs = new UploadArgs
{
FileOnly = false,
Files = base.Request.Files,
Folder = empty,
Overwrite = Settings.Upload.SimpleUploadOverwriting,
Unpack = false,
Versioned = Settings.Media.UploadAsVersionableByDefault,
Language = contentLanguage,
CloseDialogOnEnd = false,
Destination = (Settings.Media.UploadAsFiles ? UploadDestination.File : UploadDestination.Database)
};
Pipeline pipeline = PipelineFactory.GetPipeline("uiUpload");
pipeline.Start(uploadArgs);
if (uploadArgs.UploadedItems.Count > 0)
{
empty = uploadArgs.UploadedItems[0].ID.ToString();
Log.Audit(this, "Upload: {0}", StringUtil.Join(uploadArgs.UploadedItems, ", ", "Name"));
}
else
{
empty = string.Empty;
}
if (string.IsNullOrEmpty(uploadArgs.ErrorText))
{
HttpContext.Current.Response.Write("<html><head><script type=\"text/JavaScript\" language=\"javascript\">window.top.scForm.getTopModalDialog().frames[0].scForm.postRequest(\"\", \"\", \"\", 'EndUploading(\"" + empty + "\")')</script></head><body>Done</body></html>");
}
return;
}
Sitecore.Exceptions.SecurityException ex = new Sitecore.Exceptions.SecurityException("Upload ItemUri invalid");
Log.Error("ItemUri not valid. ItemUri: " + text, ex, this);
throw ex;
}
catch (OutOfMemoryException)
{
Log.Error($"Uploaded file is too big. Stack Trace: failed on OutOfMemoryException catch block of RSM.Feature.Media.sitecore.shell.Applications.Media.UploadMedia.UploadMediaPage2", this);
HttpContext.Current.Response.Write("<html><head><script type=\"text/JavaScript\" language=\"javascript\">window.top.scForm.getTopModalDialog().frames[0].scForm.postRequest(\"\", \"\", \"\", 'ShowFileTooBig(" + StringUtil.EscapeJavascriptString(base.Request.Files[0].FileName) + ")')</script></head><body>Done</body></html>");
}
catch (Exception ex3)
{
if (ex3.InnerException is OutOfMemoryException)
{
Log.Error($"Uploaded file is too big. Stack Trace: failed on OutOfMemoryException with ex3 catch block of RSM.Feature.Media.sitecore.shell.Applications.Media.UploadMedia.UploadMediaPage2", this);
HttpContext.Current.Response.Write("<html><head><script type=\"text/JavaScript\" language=\"javascript\">window.top.scForm.getTopModalDialog().frames[0].scForm.postRequest(\"\", \"\", \"\", 'ShowFileTooBig(" + StringUtil.EscapeJavascriptString(base.Request.Files[0].FileName) + ")')</script></head><body>Done</body></html>");
}
else if (ex3.InnerException is DuplicateItemNameException)
{
Log.Error($"File with same name already exist.", this);
HttpContext.Current.Response.Write("<html><head><script type=\"text/JavaScript\" language=\"javascript\">window.top.scForm.getTopModalDialog().frames[0].scForm.postRequest(\"\", \"\", \"\", 'ShowUploadError(\"" + "The item with same name is already defined on this level.\"" + ", " + StringUtil.EscapeJavascriptString(base.Request.Files[0].FileName) + ")')</script></head><body>Done</body></html>");
}
else
{
Log.Error($"An error occured while uploading the media. The exception is {ex3.Message}", this);
HttpContext.Current.Response.Write("<html><head><script type=\"text/JavaScript\" language=\"javascript\">window.top.scForm.getTopModalDialog().frames[0].scForm.postRequest(\"\", \"\", \"\", 'ShowError')</script></head><body>Done</body></html>");
}
}
}
}
}
}
Now replace the existing UploadMedia2.aspx with your version and check according to the exception the media dialogues will show the user-friendly messages.
For more info with screenshots please check this helpful blog https://www.nehemiahj.com/2022/07/sitecore-media-upload-dialog-duplicate.html
Thanks!!