Developer forum

Forum » Templates » How can I loop through images in a folder, based on a File field in eCom Product

How can I loop through images in a folder, based on a File field in eCom Product

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

As a progression on some of my previous questions, I am trying to loop through the files of a folder and displaying them in a list.

All this is based on eCom product custom field, with the tag and systems name Images.

My not working code so far is like this:

Hi Again

 

I am trying to grab this inside an foreach loop, and theres proably a whole lot wrong with my code, but maybe anyone would be so kind to indicate what I am doing wrong?

 

My code is

 

@foreach (LoopItem i in GetLoop("Products")){
        

string input = i.GetValue("Ecom:Product:Field.Images.Clean");
string folder = input.Substring(0, input.LastIndexOf("/"));


   foreach(var file in GetLoop(folder.ListOfFiles)){

    GetValue(folder)/@file.GetValue(folder.FileName);
 
    }
 
    }

 

But this doesn´t work, and the error I get is the following:

 

Line 28: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
Line 32: 'string' does not contain a definition for 'ListOfFiles' and no extension method 'ListOfFiles' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)


Replies

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Solved it with this

 


 
@foreach (LoopItem i in GetLoop("Products")){


    string input = i.GetValue("Ecom:Product:Field.Images.Clean").ToString();
if (input != "") {
string domain = "http://www.skyn.fo";
string comma = ", ";
 string folder = input.Substring(0, input.LastIndexOf("/")).ToString();
string folderOnDisk = System.Web.HttpContext.Current.Server.MapPath(folder);
  var directoryInfo = new System.IO.DirectoryInfo(folderOnDisk);
  foreach (var fileInfo in directoryInfo.GetFiles("*.jpg")){
@domain@(string.Format("{0}/{1}", folder,  fileInfo.Name))@comma

              }
        }
    }

 

 

You must be logged in to post in the forum