Skip to content

Attempt to index local 'form' (a nil value) #26

@zayednetad

Description

@zayednetad

Hi,
I am following below code on fedora Linux with nginx:

        local resty_sha256 = require "resty.sha256"
        local upload = require "resty.upload"

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

        local upload_path = '/tmp/uploads/'


        while true do
           local typ, res, err = form:read()

            if not typ then
                ngx.say("failed to read: ", err)
                return
            end

            if typ == "header" then
                local file_name = upload_path .. ngx.time()
                if file_name then
                    file = io.open(file_name, 'w+')
                    if not file then
                        ngx.say("failed to open file ", file_name)
                        return
                    end
                end
            elseif typ == "body" then
                if file then
                    file:write(res)
                    sha256:update(res)
                end
            elseif typ == "part_end" then
                file:close()
                file = nil
            elseif typ == "eof" then
                break
            else
                -- nothing
            end
        end

        local digest = sha256:final()
        sha256:reset()

        local str = require "resty.string"
        ngx.say("sha256: ", str.to_hex(digest))```

When I run this as

curl -F "filename=@shatest.lua" http://127.0.0.1:8089/uploadtest

It gives me below errors:

$ tail -f /usr/local/openresty/nginx/logs/error.log 2016/06/29 15:26:23 [error] 31449#0: *12 lua entry thread aborted: runtime error: /usr/local/openresty/nginx/uploadtest.lua:13: attempt to index local 'form' (a nil value) stack traceback: coroutine 0: /usr/local/openresty/nginx/uploadtest.lua: in function </usr/local/openresty/nginx/uploadtest.lua:1>, client: 127.0.0.1, server: localhost, request: "POST /uploadtest HTTP/1.1", host: "127.0.0.1:8089"

Can you please help me what is wrong here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions