首先用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))

posted on 2014-11-19 21:19  linxiong  阅读(400)  评论(0编辑  收藏  举报