Copied to clipboard

Flag this post as spam?

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


  • Høgni 16 posts 66 karma points
    Nov 27, 2013 @ 20:55
    Høgni
    0

    Show list of PDF's in Razor

    Hi. I have this problem. In the Media Section i have a folder called "pdf" and another folder in the pdf folder called "news". In the news folder i have oploaded a few pdf's which i want to be displayed on my website

    Hope there is someone out there there can help me. 

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 27, 2013 @ 22:07
    Dennis Aaen
    0

    Hi Høgni,

    If you´re using Webforms, here is a way that you could do it,

    On the document type create a field of the data type media picker and give it a name and an alias. Then the user picked the folder called news. With this code below you will display the files in the news folder.

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @if (Model.HasValue("Alias")){

         var MediaFolder = Library.NodeById(Model.Alias);
        
         foreach(var file in MediaFolder.Children){
             var fileUrl = file.umbracoFile;
            
          <a href="@imageUrl">
            @file.Name
          </a>

             }
     }

    If you want to show the extension of the file too it can be done like this:

    @if (Model.HasValue("Alias")){

         var MediaFolder = Library.NodeById(Model.Alias);
        
         foreach(var file in MediaFolder.Children){
             var fileUrl = file.umbracoFile;
            
          <a href="@fileUrl">
             @[email protected]
          </a>
             }
     }

    Hope this can help you, remember to change the locations where I have written Alias. Change it so it match the alias of your field on the document type where the user has to pick the folder.

    /Dennis.

  • Høgni 16 posts 66 karma points
    Nov 27, 2013 @ 22:44
    Høgni
    0

    Hi thank you for the quick answer... I'm still pretty new to this :/

    I dont quite understand your answer

    /Hogni 

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 27, 2013 @ 23:23
    Dennis Aaen
    100

    Hi Hogni,

    That´s okay. I will try to explain it more detailed then.

    I assume you are using Webforms and macro scripts, if so. Find the page at you want to show the list of the PDF files. Then find the document type for that type of page. You will find the document types under the settings sections -- > document types.

    Then add a new propperty to the document type, give a name and it will get an alias. Choose the field to be a type of media picker. When you have done that save the document type. Go to the developer section, create a clean scripting file with a associated macro file. In the scripting file pasted this code in.

    @if(Model.HasValue("Alias")){

         
    var MediaFolder = Library.NodeById(Model.Alias);
         
         
    foreach(var file in MediaFolder.Children){
             
    var fileUrl = file.umbracoFile;
             
         
    <a href="@fileUrl">
           
    @file.Name
         </a>

             
    }
     
    }


    Remember to change Alias so it match your alias of the file on document type. e.g if you name of the field is: Pick Files it will get an alias of pickFiles. So all the places where I have wrote Alias you have to change it to. Model.HasValue("pickFiles").

    If you want the list to print the extention of the file you could use e.g the name would be your name of the file.pdf

    @if(Model.HasValue("Alias")){

         
    var MediaFolder = Library.NodeById(Model.Alias);
         
         
    foreach(var file in MediaFolder.Children){
             
    var fileUrl = file.umbracoFile;
             
         
    <a href="@fileUrl">
             
    @file.Name.@file.umbracoExtension
          </a>
             
    }
     
    }

    When you have created the scription file with the associated macro file go back to the settings section, find the template for the page you want the list of your file to appear and insert the macro. e.g  or use the Umbraco backoffice to insert the macro. Then go to the content section, choose the a page with new field, Then pick the news folder with the new media picker field, and it will list the files in the folder.

     
    I don´t hope that this post is too detailed, but makes more sense now to you.
    /Dennis
  • Høgni 16 posts 66 karma points
    Dec 03, 2013 @ 14:59
    Høgni
    0

    Hi thanks againg for the great answer :) just that i cant get the URL to work :/

    @if (Model.Children.Where("Visible").Any())
    

    {

        <div class="article_pdf">
    
    
        @foreach (var childPage in Model.Children.Where("Visible"))
        {
            var fileUrl = childPage.umbracoFile; 
    
                <a href="@fileUrl" class="article_pdf" rel="">
                    @childPage.title
                </a>
        }
                </div>
    

    }

    /Hogni

  • Chris C 43 posts 184 karma points
    Dec 03, 2013 @ 18:22
    Chris C
    0

    Does @fileUrl.Url work? 

  • Høgni 16 posts 66 karma points
    Dec 04, 2013 @ 09:11
    Høgni
    0

    nope :/

Please Sign in or register to post replies

Write your reply to:

Draft