4

I'm using Tridion 5.3, and I'm trying to check the Multimedia filesize in the OnComponentSavePre event (using a C# event handler) using Component.Multimedia.FileSize

I'm sure that I have done this before, but now I get

'Unable to get filesize of Unknown'...

Any suggestions?

5

Probably because the file is not yet saved, and therefore Tridion can't determine its size?

You could try something like this instead:

MemoryStream ImageStream = new MemoryStream(Component.Multimedia.GetBinaryContent());
MMImage = Image.FromStream(ImageStream);
Component.MetadataFields[IMAGE_WIDTH_FIELD].value[1] = MMImage.Size.Width.ToString();

UPDATE

I wrote this for a 5.3 Event System, not sure it would work on later versions... What I do is I try to load the image from its "Multimedia Filename". This is the path that the image was uploaded from, but given that it might have been uploaded from a client's workstation, the file may not exist on the server. In that case, I try to load the image from the web\upload folder (where the Tridion CME would have copied the image to before saving it in a component).

Image image = null;
if(Component.Info.Version < 1) // New Component
{
    string fileName = null;
    string uploadFolder = null;
    if(!File.Exists(Component.Multimedia.Filename))
    {
        uploadFolder = GetTridionInstallPath() + @"web\Upload\"; //Dragons be here
        fileName = uploadFolder + Path.GetFileName(Component.Multimedia.Filename);
    }
    else
    {
        fileName = Component.Multimedia.Filename;
    }
    if(File.Exists(fileName))
    {
        FileStream imageStream = File.OpenRead(fileName);
        image = Image.FromStream(imageStream);
    }
    else
    {
        logger.logEvent("Could not find file " + filename, EnumSeverity.severityInfo);
    }
}
else
{
    MemoryStream imageStream = new MemoryStream(Component.Multimedia.GetBinaryContent());
    image = Image.FromStream(imageStream);
}
if(image != null)
{
    try
    {
        Component.MetadataFields[IMAGE_WIDTH_FIELD].value[1] = MMImage.Size.Width.ToString();
        Component.MetadataFields[IMAGE_HEIGTH_FIELD].value[1] = MMImage.Size.Height.ToString();
    }
    catch (Exception ex)
    {
        logger.logEvent("Could not get image dimensions, first version? Error: " + ex.ToString(), EnumSeverity.severityError);
    }
}
|improve this answer|||||
  • I can't even get the 'BinaryContent' at this point. – Paul Durdin May 13 '13 at 14:50
  • You're right. Let me update my answer. – Nuno Linhares May 13 '13 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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