请求:取指定目次下面的一切图片,以表格的型式展现并显现该图片的相对途径。
服务端代码:
public partial class ViewIcon : System.Web.UI.Page { JArray ja = new JArray(); //定义一个数组 public string info = string.Empty; protected void Page_Load(object sender, EventArgs e) { var path1 = System.AppDomain.CurrentDomain.BaseDirectory;//猎取顺序集目次 string path = Path.Combine(path1, "Image", "menu");//Path.Combine 将3个字符串组合成途径 var images = Directory.GetFiles(path, ".", SearchOption.AllDirectories).Where(s => s.EndsWith(".png") || s.EndsWith(".jpg") || s.EndsWith(".gif")); //images = Directory.GetFiles(path, "*.png|*.jpg", SearchOption.AllDirectories); //Directory.GetFiles 返回指定目次的文件途径 SearchOption.AllDirectories 指定搜刮当前目次及子目次 //遍历string 型 images数组 foreach (var i in images){ var str = i.Replace(path1, "");//猎取相对途径 var path2 = str.Replace("\\", "/");将字符“\\”转换为“/” ja.Add(path2); } info = Newtonsoft.Json.JsonConvert.SerializeObject(ja);//序列化为String } }
前端代码:
<script type="text/javascript"> $(function(){ var images = <%=info%>; var list = []; list.push("<table>"); list.push("<thead>"); list.push("<tr>"); list.push("<td>图标</td>"); list.push("<td>途径</td>"); list.push("<td>图标</td>"); list.push("<td>途径</td>"); list.push("</tr>"); list.push("</thead>"); list.push("<tbody>"); $.each(images, function (a,b) { if((a+1)%2==0){ list.push("<td>"+"<img width='50' height='50' src = '../../" + b + "'></td>"); list.push("<td>"+b+"</td>"); list.push("</tr>"); } if((a+1)%2!=0){ list.push("<tr>"); list.push("<td>"+"<img width='50' height='50' src = '../../" + b + "'></td>"); list.push("<td>"+b+"</td>"); } }) list.push("</tbody>"); list.push("</table>"); list.push("<br>"); var images = list.join(""); $("#imgs").append(images); }) </script>
效果图以下:
以上就是完成C#遍历文件夹及子目次下一切图片的示例代码分享的细致内容,更多请关注ki4网别的相干文章!