Copied to clipboard

Flag this post as spam?

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


  • Bry 13 posts 93 karma points
    Apr 09, 2019 @ 08:27
    Bry
    0

    Getting the File Type/Format of a file

    Hello,

    I am looking to create an if statement that would allow me to check if a file that the user has attached to a piece of content via a media picker is of a specific file format. i.e. ".mp4"

    If a user uploads a video, I can wrap the video html within an if statement that would check if there is a video file attached then show the video embed.

    I hope that makes sense.

  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 09, 2019 @ 09:12
    Jonathan Distenfeld
    0

    Hi Bry,

    you can check for the build-in umbraco property "UmbracoExtension".

    So it would look like this:

    @if(Model.Media.UmbracoExtension == "mp4")
    {
    // do something
    }
    

    -Jonathan

  • Bry 13 posts 93 karma points
    Apr 09, 2019 @ 13:30
    Bry
    0

    Thank you for your reply Jonathan, however when using that code I get the following error

    CS1061: 'IPublishedContent' does not contain a definition for 'Media' and no accessible extension method 'Media' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
    
  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 09, 2019 @ 15:09
    Jonathan Distenfeld
    0

    "Media" is an example. You'll have to replace "Media" with your own property alias.

    Is your model builder enabled? If not, you will have to do it like this:

    @if(Model.Value("yourPropertyAlias") != null && Model.Value("yourPropertyAlias").UmbracoExtension == "mp4")
    {
    // do something
    }
    
  • Bry 13 posts 93 karma points
    Apr 10, 2019 @ 10:32
    Bry
    0

    Yeah just for the record the model builder is enabled. But even with the suggested code I am getting:

    CS1061: 'object' does not contain a definition for 'UmbracoExtension' and no accessible extension method 'UmbracoExtension' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
    
  • Bry 13 posts 93 karma points
    Apr 10, 2019 @ 13:25
    Bry
    0

    Resolved!

  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 26, 2019 @ 12:27
    Jonathan Distenfeld
    0

    Great to here you got it working!

    Would you mind providing your solution or marking the answer that helped you solving your problem as solution for others that are facing the same problem?

    Jonathan

  • Nicolai Christiansen 4 posts 96 karma points
    Oct 04, 2019 @ 09:12
    Nicolai Christiansen
    0

    You could do something like this

    var file = Model.Value

    @if (file.Value("umbracoExtension") == "mp4" ) { // do something }

  • George Phillipson 108 posts 287 karma points
    Apr 26, 2019 @ 22:27
    George Phillipson
    0

    Another non-Umbraco way is MimeMapping which does not rely on the file extension.

    An example is

    string mimeType = MimeMapping.GetMimeMapping(file.mp4);
    
    if (mimeType == "video/mp4")
        {
        //Do whatever
        }
    

    In .NET 4.5+ just use System.Web.MimeMapping.GetMimeMapping which can be found in

    Namespace: System.Web

    Assembly: System.Web.dll

    https://referencesource.microsoft.com/#system.web/MimeMapping.cs

Please Sign in or register to post replies

Write your reply to:

Draft