openresty / lua-resty-upload

Streaming reader and parser for http file uploading based on ngx_lua cosocket
405 stars 113 forks source link

has a bug on windows,Write a file will be more than one \ r in each line #31

Closed haochun closed 7 years ago

haochun commented 7 years ago

when i use the mouble,i occured a probole.it will be more than one \r in each line.my lua script:

package.path = 'E:/nginx-windows/lua/?.lua;E:/nginx-windows/resty/?.lua;'
package.cpath = 'E:/nginx-windows/?.dll;'

local restyUpload = require "upload"  

local chunk_size = 4096  

local file  
local filelen=0  
local filename
local args = ngx.req.get_query_args()  
--ngx.say(args) 
function get_filename(res)  
    local filename = ngx.re.match(res,'(.+)filename="(.+)"(.*)')  
    if filename then   
        return filename[2]  
    end  
end  

local rootDir = "e:/temp/"  
local filepath = rootDir .. "/" --args.path .. 
local temp = io.open(filepath)

local form = restyUpload:new(chunk_size)
if form == nil then
    ngx.log(ngx.ALERT, "parse body failed.")
    ngx.exit(500)
end

form:set_timeout(0) -- 1 sec  
local i=0  
ngx.header.content_type = "text/plain"
while true do  
    local typ, res, err = form:read()  
    if not typ then  
        ngx.say("failed to read body: ", err)  
        return  
    end
    if typ == "header" then  
        if res[1] ~= "Content-Type" then  
            filename = get_filename(res[2])  
            if filename then  
                i=i+1  
                filepath = filepath .. filename  
                file = io.open(filepath,"w+")  
                if not file then
                    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
                    ngx.log(ngx.ALERT, "can not open file: "..filepath)
                    return  
                end  
            end  
        end  
    elseif typ == "body" then 
        ngx.say("type", res);
        if file then  
            --filelen= filelen + tonumber(string.len(res))      
            file:write(res)
        end  
    elseif typ == "part_end" then  
        if file then  
            file:close()  
            file = nil
            ngx.say("success")  
        end  
    elseif typ == "eof" then  
        break  
    else  
    end  
end  
if i==0 then
    ngx.exit(ngx.HTTP_NOT_FOUND)    
    ngx.log(ngx.ALERT, "please upload at least one file!")
    return  
end

the response is:

type<html>
<body>
<form enctype="multipart/form-data" method="post"  action="http://localhost:8090/upload">
    <input type="file" name="file"/>
    <input type="submit" value="upload"/>
</form>

</body>

</html>
success

but,the file is:

<html>

<body>

<form enctype="multipart/form-data" method="post"  action="http://localhost:8090/upload">

    <input type="file" name="file"/>

    <input type="submit" value="upload"/>

</form>

</body>

</html>

so,when i try to use this script to upload image、zip、doc and other files ,all files are damage.and i found that not all files will damage,same file is right.

agentzh commented 7 years ago

@haochun You need to open the file in binary mode instead of the default text mode on Windows.

agentzh commented 7 years ago

@haochun See #25 for the correct way of opening (binary) files in Lua.

haochun commented 7 years ago

thank you very much!it is work.