ajaxFileUpload + lua-resty-upload 上传文件

简介: ajaxFileUpload下载地址 地址:http://pan.baidu.com/s/1mgJypz6 html页面 DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.

ajaxFileUpload下载地址

地址:http://pan.baidu.com/s/1mgJypz6

html页面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Untitled</title>
    <script src="jquery-1.7.1.js"></script>
    <script src="ajaxfileupload.js"></script>
     <script type="text/javascript">
        $(function () {
            $(":button").click(function () {
                ajaxFileUpload();
            })
        })
        function ajaxFileUpload() {
            $.ajaxFileUpload
            (
                {
                    url: '/dsideal_yy/rating/test', //用于文件上传的服务器端请求地址                    
                    fileElementId: 'file1', //文件上传空间的id属性  <input type="file" id="file" name="file" />
                    dataType: 'json', //返回值类型 一般设置为json
                    success: function (data)  //服务器成功响应处理函数
                    {                        
                        alert(data.msg);
                    }
                }
            )
            return false;
        }
    </script>
  </head>
  <body>
    <body>
        <p><input type="file" id="file1" name="file" /></p>
        <input type="button" value="上传" />
    </body>    
</html>

lua代码

local upload = require "resty.upload"
local uuid =  require "resty.uuid";

local chunk_size = 4096
local form = upload:new(chunk_size)
local file

--获取文件名
function get_filename(res) 
    local filename = ngx.re.match(res,'(.+)filename="(.+)"(.*)') 
    if filename then  
        return filename[2] 
    end 
end 

--获取文件扩展名
function getExtension(str)
    return str:match(".+%.(%w+)$")
end

while true do
    local typ, res, err = form:read()    
    if not typ then         
         ngx.say("{error:'error', msg:'"..tostring(err).."',imgurl:''}")
         return
    end
    if typ == "header" then
        if res[1] ~= "Content-Type" then
            local file_id = uuid.new()
            local filen_ame = get_filename(res[2])
            local extension = getExtension(filen_ame)
            local dir = string.sub(file_id,0,2)            
            local file_name = "/usr/local/tomcat7/webapps/dsideal_yy/html/down/Material/"..dir.."/"..file_id.."."..extension            
            if file_name then
                file = io.open(file_name, "w+")
                if not file then
                    ngx.say("{error:'error', msg:'failed to open file',imgurl:''}")
                    return
                end
            end
        end
     elseif typ == "body" then
        if file then
            file:write(res)            
        end
    elseif typ == "part_end" then
        file:close()
        file = nil
    elseif typ == "eof" then
        break
    else
        -- do nothing
    end
end

ngx.say("{error:'success', msg:'上传成功!',imgurl:''}")

 

相关文章
|
JavaScript
js 使用fetch来上传文件 formdata()
js 使用fetch来上传文件 formdata()
PHP+ajaxfileupload与jcrop插件结合 完成头像上传
这个地方我本来想做个上传进度的效果,但技术有限失败了。上传按钮我还做了一个文件大小的限制,但是由于浏览器兼容性的问题,不完美在IE6--IE9之间还有很多问题需要解决
|
JavaScript Java 应用服务中间件
Tomcat 是怎么处理js file access request的
Sent: Tuesday, November 25, 2014 12:08 PM
107 0
Tomcat 是怎么处理js file access request的
|
Web App开发 PHP 数据安全/隐私保护