Chinaunix首页 | 论坛 | 博客
  • 博客访问: 357999
  • 博文数量: 90
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 113
  • 用 户 组: 普通用户
  • 注册时间: 2016-01-27 19:56
文章分类

全部博文(90)

文章存档

2017年(12)

2016年(78)

分类: 嵌入式

2016-09-07 15:06:27


点击(此处)折叠或打开

  1. module("luci.controller.baoservice.baoservice",package.seeall) --表示lua程序的入口
  2. local h=require "luci.http"
  3. local fs=require "nixio.fs"

  4. function index()

  5.     entry({"baoservice","bao"},call("bao_service"),"service",10 ).dependent=false
  6. end

  7. function bao_service()

  8.      local flag = true
  9.      local run = true
  10.      local fd = nil
  11.     local up="up"
  12.     local ss="ss"
  13.      -- 在这之前是不能有任何语句,声明语句除外 如果这里有formvalue函数,将不会执行setfilehandler函数--理解回调函数
  14.      h.setfilehandler(
  15.          function(field, chunk, eof)
  16.             filename=field.file
  17.             
  18.             h.write("start\r\n")
  19.             --h.write(field.."\r\n") -- -> field :a table value 如:file (0xa5e140) 文件句柄
  20.             h.write(field.file.."\r\n") -- 输出文件的名字
  21.             h.write(field.name.."\r\n")     --输出post 的name 属性值
  22.             --h.write(chunk.."\r\n")     --输出的是文件流
  23.             --h.write(eof.."\r\n") -- -> eof :a boolean value 文件结尾标志,文件流传输结束为true 否则为false
  24.              if not field or not run then return end
  25.             --h.write("start up\r\n")
  26.              if flag then
  27.                 
  28.                  flag = false
  29.              end
  30.  
  31.              -- 将上传的文件保存到/tmp目录下
  32.              local path = "/tmp/" .. field.name
  33.             --local path ="/tmp/data"
  34.              if not fd then
  35.                  fd = io.open(path, "w")
  36.              end
  37.  
  38.              fd:write(chunk)
  39.  
  40.              if eof and fd then
  41.                  fd:close()
  42.                  fd = nil
  43.  
  44.                  h.write("upload success\r\n")
  45.              end
  46.          end
  47.      )
  48.         local lon=h.formvalue("route_long")
  49.         h.write("formvalue\r\n")
  50.         
  51.         if lon==nil then lon=0 end
  52.         
  53.     h.write("end\r\n"..lon.."\r\n")
  54.     
  55.     print(filename)
  56.     
  57.     end



  • ----------------以下是分析--------------------------    
  •     setfilehandler函数是调用一个回调函数(特别注意:当在bao_service函数中,如果没有调用formvalue函数,setfilehandler函数是不会执行的,而且至少有一个formvalue函数在setfilehandler函数的外面),回调函数有三个参数,具体见上面
  •     ----
  •     尝试1:post内容
  • -------------------------------------------
  • POST /cgi-bin/luci/baoservice/bao HTTP/1.1
  • Host: 192.168.10.1
  • Cache-Control: no-cache

  • ----WebKitFormBoundaryE19zNvXGzXaLvS5C
  • Content-Disposition: form-data; name="route_long"

  • 111111
  • ----WebKitFormBoundaryE19zNvXGzXaLvS5C
  • Content-Disposition: form-data; name="haha"; filename="testsethandle.txt"
  • Content-Type: text/plain


  • ----WebKitFormBoundaryE19zNvXGzXaLvS5C

  • -------------------------------------------
  •     回显内容1:

  • -------------------------------------------
  • start
  • testsethandle.txt
  • haha
  • upload success
  • formvalue
  • end
  • 111111
  • testsethandle.txt
  • --------------------------------------------



  • 尝试2:post内容
  • -------------------------------------------
  • POST /cgi-bin/luci/baoservice/bao HTTP/1.1
  • Host: 192.168.10.1
  • Cache-Control: no-cache

  • ----WebKitFormBoundaryE19zNvXGzXaLvS5C
  • Content-Disposition: form-data; name="haha2"; filename="testsethandle.txt"
  • Content-Type: text/plain


  • ----WebKitFormBoundaryE19zNvXGzXaLvS5C
  • Content-Disposition: form-data; name="route_long"

  • 222222222222
  • ----WebKitFormBoundaryE19zNvXGzXaLvS5C

  • -------------------------------------------
  • 回显内容2:

  • -------------------------------------------
  • start
  • testsethandle.txt
  • haha2
  • upload success
  • formvalue
  • end
  • 222222222222
  • testsethandle.txt
  • -------------------------------------------

  • 尝试3:post内容
  • -------------------------------------------

  • POST /cgi-bin/luci/baoservice/bao HTTP/1.1
  • Host: 192.168.10.1
  • Cache-Control: no-cache

  • ----WebKitFormBoundaryE19zNvXGzXaLvS5C
  • Content-Disposition: form-data; name="route_long"

  • 222222222222
  • ----WebKitFormBoundaryE19zNvXGzXaLvS5C

  • 回显内容3:

  • -------------------------------------------
  • formvalue
  • end
  • 222222222222
  • nil
  • -------------------------------------------    

  • 分析:当post没有filename时,即没有post文件时,setfilehandler函数不会被执行,
  • 这很明显,setfilehandler是专门处理文件上传的,没文件当然不处理。chunk为文件块,每块差不多为2k 多
  • 当post有filename时,setfilehandler函数会优先处理文件,再处理普通字符串。

  • 阅读(3345) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~