Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 15, 2011 @ 16:23
    Matt Brailsford
    0

    Best way to check whether a media file already exists?

    Hey Guys,

    What would you say is the best way to check whether a media file already exists within a folder in the media section?

    ie given the node ID of a media folder, and a file name, I want to know if a media item exists with an umbracoFile property whith the same name as the given file name.

    My gut feeling is to just query the database direct to save itterating over all the media items, but anybody else got any other suggestions?

    Matt

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Feb 16, 2011 @ 18:14
    Bo Damgaard Mortensen
    0

    Hi Matt,

    The following code is not tested, but maybe something like this could do the trick? If you have the node id of the media folder and the filename of the file:

    Media rootFolder = new Media(1020);
    
    var media = from m in rootFolder.Children
                where m.getProperty("umbracoFile").Value.ToString().Contains("nameOfFile")
                select m;
    
    if (media != null)
    {
        // do work
    }
    else
    {
            // do work
    }

    .. just a shot in the dark really ;-)

    All the best,

    Bo

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 17, 2011 @ 11:19
    Matt Brailsford
    0

    Hey Bo, 

    That is pretty much how I ended up doing it. I was just going to hit the DB to save itterating the child media items, but had to itterate them anyway as I'd need the property ID for the query (and some other checks I'm doing).

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft