Copied to clipboard

Flag this post as spam?

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


  • Rayyan 46 posts 238 karma points
    Jul 19, 2017 @ 11:43
    Rayyan
    0

    Display images from directory

    Hello Everyone, I want to display images from specific directory in the server, I have got this code in the partial view but didn't work, any idea please?

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
        string folderPath = Server.MapPath("~/media");
        string[] files = Directory.GetFiles(/1039);    
    
            @foreach (string item in files){
                <img src="/media/@Path.GetFileName(item)"/>
        } 
    
  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Jul 19, 2017 @ 11:47
    Alex Skrypnyk
    100

    Hi Rayyan

    Try this code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        string folderPath = Server.MapPath("~/media");
        string[] files = Directory.GetFiles(folderPath + "/1039");
    }
    @foreach (string item in files){
        <img src="@item"/>
    } 
    

    The question is why do you need to show media images .net api? You can do it with Umbraco helper.

    Thanks,

    Alex

  • Rayyan 46 posts 238 karma points
    Jul 19, 2017 @ 11:55
    Rayyan
    0

    Hi Alex, I really appreciate your help. I'm going with this way because I have hundreds of folders and i need to get the images from each folder dynamically.

    It seems the code you suggest is working, but I have a problem with the image PATH, please advise;

    E:\web\domain.com\media/1039\homepro.jpg

    Thank you very much

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Jul 19, 2017 @ 11:58
    Alex Skrypnyk
    1

    Sorry, this code should work:

    @foreach (string item in files)
    {
        <img src="/media/@Path.GetFileName(item)" />
    } 
    
  • Rayyan 46 posts 238 karma points
    Jul 19, 2017 @ 12:14
    Rayyan
    0

    Working perfectly :) Thanks a million Alex!

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        string folderPath = Server.MapPath("~/media");
        string[] files = Directory.GetFiles(folderPath + "/1039");
    }
    @foreach (string item in files){
         <img src="/media/1039/@Path.GetFileName(item)" />
    } 
    
  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Jul 19, 2017 @ 12:15
    Alex Skrypnyk
    1

    You are welcome, Rayyan.

    But be aware that it's not a right way to show the image, imagine if somebody will change this image from Umbraco backend?

  • Rayyan 46 posts 238 karma points
    Jul 19, 2017 @ 12:23
    Rayyan
    0

    What do you suggest for my case, having hundreds of folders and I need to display each folder images in specific page?

    Thanks

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Jul 19, 2017 @ 15:24
    Alex Skrypnyk
    2

    The best case will be to use media picker on the page, and select folder for each page and render with Umbraco helper.

  • Rayyan 46 posts 238 karma points
    Jul 19, 2017 @ 16:28
    Rayyan
    0

    Thank you Alex, I will consider that in the next version. And for the same code I have tried to add the folder name from PropertyValue but it dosen't work, could you please check the following;

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        string folderPath = Server.MapPath("/media");
        string[] files = Directory.GetFiles(folderPath + "/@Model.Content.GetPropertyValue("placeID")");
    }
    @foreach (string item in files){
        <img src="/media/@Model.Content.GetPropertyValue("placeID")/@Path.GetFileName(item)" />
    } 
    

    Am I doing it in the wrong way?

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Jul 19, 2017 @ 22:02
    Alex Skrypnyk
    0

    Hi Rayyan

    What is placeID property? How did you store it in Umbraco?

    Alex

  • Rayyan 46 posts 238 karma points
    Jul 19, 2017 @ 22:05
  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Jul 19, 2017 @ 22:16
    Alex Skrypnyk
    1

    You are welcome!

    Have a nice evening.

  • Rayyan 46 posts 238 karma points
    Jul 19, 2017 @ 22:18
    Rayyan
    1

    Your help made my day! Have a nice evening you too.

  • Rayyan 46 posts 238 karma points
    Jul 20, 2017 @ 11:07
    Rayyan
    0

    Hi Alex,

    Sorry for bothering you, but I have a question...

    Is there a way to control the number of the items that the code get from the folder?

    Something as below;

     <img src="/media/@Model.Content.GetPropertyValue("placeID")/@Path.GetFileName(item) [6 Items Max] " />
    

    Many thanks

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Aug 01, 2017 @ 10:42
    Alex Skrypnyk
    0

    Hi Rayyan

    Did you solve the issue? Can you share with out community?

    Thanks,

    Alex

  • Rayyan 46 posts 238 karma points
    Aug 01, 2017 @ 11:36
  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Aug 01, 2017 @ 11:37
    Alex Skrypnyk
    0

    Hi Rayyan,

    Thanks for sharing, is it working for you?

    Alex

  • Rayyan 46 posts 238 karma points
    Aug 01, 2017 @ 11:45
    Rayyan
    1

    Yes, perfectly!

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Jul 20, 2017 @ 11:11
    Alex Skrypnyk
    0

    Hi Rayyan

    Of course you can do it:

    string[] files = Directory.GetFiles(folderPath).Take(6).ToArray();
    

    Thanks,

    Alex

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies