Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lose some data when use lua-resty-upload to write to a file. #11

Closed
itsre opened this issue Jan 24, 2015 · 0 comments
Closed

Lose some data when use lua-resty-upload to write to a file. #11

itsre opened this issue Jan 24, 2015 · 0 comments

Comments

@itsre
Copy link

itsre commented Jan 24, 2015

When I upload a file via lua-resty-upload ,I find the lastest uploaded file always lose some data,but when I upload another file, the previous file seems ok,it seems something has been buffered and not flush to the disk immediately。

    local upload = require "resty.upload"
    local chunk_size = 8192
    local form = upload:new(chunk_size)
    local uuid = require("resty.uuid")
    local file
    local currenttime = os.time()
    local filename = uuid().."-"..pid
    local file_name = uploadpath..filename
    if form then
    while true do
        local typ, res, err = form:read()
        if not typ then
              ngx.say(cjson.encode({code=1,message="nofile upload"}))
             return
        end

            if file_name then
                file = io.open(file_name, "a+")
                if not file then
                    ngx.say(cjson.encode({code=2,message="file save failed"}))
                    return
                end
            end


         if typ == "body" then
            if file then
                file:write(res)
            end

        elseif typ == "part_end" then
            file:close()
            file = nil
            file_name = nil
            ngx.say(cjson.encode({code=0,url=ngx.var.scheme.."://"..ngx.var.host.."/"..filename}))
            filename = nil
            return

        elseif typ == "eof" then
            break

        else
            -- do nothing
        end

    end
    else
        ngx.say(cjson.encode({code=1,message="nofile upload"}))
        return
    end

I call file:flush() before file:colse() ,there is no effect

@itsre itsre closed this as completed Jan 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant