在OpenWrt中上传文件至路由器

上传功能的整体逻辑比较简单,主要是对luci.http.setfilehandler方法的使用。对于这个方法,有一个比较特殊的地方即这个方法一定要放在最前面(除声明语句)。

 1 <%
2 local h = require "luci.http"
3 local io = require "nixio"
4 local flag = true
5 local run = true
6 local fd = nil
7
8 -- 在这之前是不能有任何语句,声明语句除外
9 h.setfilehandler(
10 function(field, chunk, eof)
11 if not field or not run then return end
12
13 if flag then
14 h.write("上传中")
15 flag = false
16 end
17
18 -- 将上传的文件保存到根目录下
19 local path = "/" .. field.file
20
21 if not fd then
22 fd = io.open(path, "w")
23 end
24
25 fd:write(chunk)
26
27 if eof and fd then
28 fd:close()
29 fd = nil
30
31 h.write("<br />上传完成")
32 end
33 end
34 )
35
36 -- 这块代码一定也要放在setfilehandler下面。
37 if h.formvalue("act") == "update" then
38 return
39 end
40 %>
41 <form id="update" name="update" action="<%=REQUEST_URI%>" method="post" enctype="multipart/form-data">
42 <input type="hidden" name="act" value="update" />
43 选择需要上传的文件:
44 <input type="file" id="updatePackage" name="updatePackage" />
45 <input type="submit" id="updateBtn" class="cbi-button cbi-button-apply" value="升级" />
46 </form>


 

posted @ 2012-04-05 13:54  AUOONG  阅读(11170)  评论(0编辑  收藏  举报