Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a lot of gif files in my specifying folder location('D\Picture\')
I want to add that gif files to creating dynamic array from specifying location using java script.

How will i solve the above problem by java scripts programming language?????

Please help me anybody for above problems.....
Posted

1 solution

Try this:

JavaScript
var images = {
  0: 'images/pic1.png',
  1: 'images/pic2.png',
  2: 'images/pic3.png'
}

function setImage(elementId, imageIndex) {
  document.getElementById(elementId).src = images[imageIndex];
}


You can create the array server side using code similar to this:

C#
string[] files = System.IO.Directory.GetFiles("d:\\picture");
        System.Text.StringBuilder sb = new StringBuilder();
        string relativePath = "images";
        int fileIndex = 0;
        sb.AppendLine("var images = {");
        foreach (string file in files)
        {
          System.IO.FileInfo fi = new FileInfo(file);
          sb.AppendLine((fileIndex > 0 ? "," : "") + fileIndex + ": '" + relativePath + "/" + fi.Name + "'");
        }
        sb.AppendLine("};");

        System.Web.UI.HtmlControls.HtmlGenericControl script = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
        script.Attributes.Add("language", "javascript");
        script.Attributes.Add("type", "text/javascript");
        script.InnerText = sb.ToString();

        this.Controls.Add(script);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900