Copied to clipboard

Flag this post as spam?

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


  • Bjørn Schaadt 9 posts 38 karma points
    Oct 22, 2014 @ 11:22
    Bjørn Schaadt
    0

    Check for file extension in mediafolder - razor

    Hi All, 

    I've created this razor scripts, which gives me all the files from within a specific mediafolder. It works okay but it could be better.

    My problem is that part of the code don't work because that part is only relevant for PDF's, since I use the package PDF thumbnails which creates a folder inside the mediafolder called pdfthumbs in which I will get the first thumbnail of the pdf.

    So in my if statement i need a conditions to check for either the file extension or, if the pdfThumbs folder exist in the mediafolder? 

    Any help will be much appreciated.

    foreach(dynamic pdfs in mediaFolder.file){
    var pdf = PDFHelper.GetMedia(pdfs.Id);
    var pdfUrl = pdf.Media.Url;
    var c = 0;
    string documentUrl = @pdfUrl;
    <div class="element-item"> <a target="_blank" href='@documentUrl1'>
                    @if (CONDITION to insert?){ foreach (var thumb in pdf.PdfThumbs){
    if(c > 0){break;}
    <img width="90" src="@thumb.fileName">
    c++;
    }
    <span>@pdf.Media.name</span>
              }
    </a>
    </div>
    }
  • Dan Lister 416 posts 1974 karma points c-trib
    Oct 22, 2014 @ 11:48
    Dan Lister
    0

    Hi Bjørn,

    You could use the following method to see if the file has a PDF extension:

    if (Path.GetExtension(pdfUrl).Equals(".pdf"))
    {
        ...
    }
    

    Thanks, Dan.

  • Charles Afford 1163 posts 1709 karma points
    Oct 29, 2014 @ 20:28
    Charles Afford
    0

    What type is mediaFolder and what type is MediaFolder.File.

    If you create a new Media or IMedia object that has a property on i think its called Extension which will give you the file extension :).

    Hope this helps ,

    Charlie :)

  • jivan thapa 194 posts 681 karma points
    Oct 29, 2014 @ 21:12
    jivan thapa
    0

    Not sure how PDFHelper.GetMedia(pdfs.Id) works. You can try this one

      var pdf = ApplicationContext.Current.Services.MediaService.GetById(pdfs.Id);
        if (pdf.HasProperty("umbracoExtension") && !string.IsNullOrEmpty(pdf.GetValue("umbracoExtension").ToString()))
        {
            if(pdf.GetValue("umbracoExtension").ToString().Equals("pdf"))
            {
                @pdf.Name
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft