zoukankan      html  css  js  c++  java
  • 【Lua】LWT遍历指定目录并输出到页面中

      首先用lua遍历目录:

     1 function getDirs(path)
     2   local s = {}
     3   function attrdir(p)                  
     4     for file in lfs.dir(p) do
     5       if file ~= "." and file ~= ".." then
     6         local f = p .. file
     7         local attr = lfs.attributes (f)
     8         if attr.mode == "directory" then        
     9             table.insert(s, f)    
    10             attrdir(f .. "/")
    11         else
    12             table.insert(s, f)
    13         end
    14       end
    15     end
    16   end
    17   attrdir(path)
    18   return s
    19 end

      然后将结果打包成JSON格式用于传递:

    1 function string2JSON(path)
    2   local s = getDirs(path)
    3   local cjson = require("cjson")
    4   local sJson = cjson.encode(s)
    5   return sJson
    6 end

      最后用lwt后台输出到页面中:

     1 require "httpd" 
     2 require "lfs"
     3 
     4 request, args = ... 
     5 
     6 function getDirs(path)
     7   local s = {}
     8   function attrdir(p)
     9     for file in lfs.dir(p) do
    10       if file ~= "." and file ~= ".." then
    11         local f = p .. file
    12         local attr = lfs.attributes (f)
    13         if attr.mode == "directory" then        
    14             table.insert(s, f)    
    15             attrdir(f .. "/")
    16         else
    17             table.insert(s, f)
    18         end
    19       end
    20     end
    21   end
    22   attrdir(path)
    23   return s
    24 end
    25 
    26 function string2JSON(path)
    27   local s = getDirs(path)
    28   local cjson = require("cjson")
    29   local sJson = cjson.encode(s)
    30   return sJson
    31 end
    32 
    33 httpd.set_content_type("text/plain") 
    34 httpd.write(string2JSON(request.filedir))

  • 相关阅读:
    名词解释
    cwoa2011项目部署到tomcat服务器(环境搭建及项目的运行)
    上网过程与原理
    C-编译器的实现
    css
    HMTL
    FIRST DAY
    关于面试的吐槽
    我的老大去创业啦
    .Net Core下使用 RSA
  • 原文地址:https://www.cnblogs.com/linxiong945/p/4109099.html
Copyright © 2011-2022 走看看