从相机/相册上传图像到服务器 - Corona sdk

时间:2012-10-07 08:01:59

标签: iphone sdk lua corona

我有一个问题,就是将图像上传到使用相机或相册拍摄的服务器。我使用以下代码将图像上传到我的服务器:但是使用此代码我的服务器只会像这样重新显示图像的表值:table:0x11791daf0,如何让它上传图像而不是价值?是否也可以使用此代码上传文本?

最后我的服务器上的PHP脚本。

提前谢谢你:)



    local function monitorMem(event)
      collectgarbage("collect")

      print( "\nMemUsage: " .. (collectgarbage("count")/1000) .. " MB")
      print("Texture Usage " .. system.getInfo( "textureMemoryUsed" ) / 1000000)

      return true
    end

    Runtime:addEventListener("enterFrame", monitorMem)




    local image 


    local mime = require "mime"

    local bkgd = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
    bkgd:setFillColor( 0, 0, 0 )

    local myRoundedRect = display.newRoundedRect(10, 50, 80, 50, 12)
    myRoundedRect.strokeWidth = 3
    myRoundedRect:setFillColor(140, 140, 140)
    myRoundedRect:setStrokeColor(180, 180, 180)

    local sessionComplete = function(event) 
            image = event.target

            print( "Camera ", ( image and "returned an image" ) or "session was cancelled" )
            print( "event name: " .. event.name )
            print( "target: " .. tostring( image ) )

            if image then
                    -- center image on screen

                    image.x = display.contentWidth/2
                    image.y = 59
                    local w = image.width
                    local h = image.height
                    image.xScale = 0.3
                    image.yScale = 0.3
                    print( "w,h = ".. w .."," .. h )
            end
    end

    local listener = function( event )
            if media.hasSource( media.Camera ) then
                    media.show( media.Camera, sessionComplete )
            else
                    native.showAlert("Corona", "Camera not found.")
            end
            return true
    end
    myRoundedRect:addEventListener( "tap", listener )


    local myRoundedRect1 = display.newRoundedRect(10, 400, 150, 50, 12)
    myRoundedRect1.strokeWidth = 3
    myRoundedRect1:setFillColor(140, 140, 140)
    myRoundedRect1:setStrokeColor(180, 180, 180)

    local Name = "Imagename"


    function uploadBinary ( filename, url, onComplete )

           -- local path = system.pathForFile( filename )
           -- local fileHandle = io.open( path, "rb" ) 
           -- if fileHandle then 

           if image then

                         local params = {
                                        body = "image_file=" .. mime.b64(tostring( image )) .. "&image_filename="..Name
                            }


               --    io.close( fileHandle )

                    local function networkListener ( event )
                            if (onComplete) then
                                            onComplete(event);
                            end
                            return true;
                    end

                    network.request( url, "POST", networkListener,  params)
            end
    end

    local function networkListener( event )
            if ( event.isError ) then
                    print( "Network error!")
            else
                  --  print ( "RESPONSE: " .. event.response)
                  print ("Working")
            end
    end

    local function Upload ()

    uploadBinary ( image, "http://www.test1.bugs3.com/Corona.php", networkListener)
    end 




    myRoundedRect1:addEventListener( "tap", Upload )





    ?php
            $image_file = fopen($_POST['image_filename'], 'wb');

            $encodedData = str_replace(' ','+',$_POST['image_file']);
            $decocedData = base64_decode($encodedData);

            fwrite($image_file, $decocedData);
            fclose($image_file);


    ?

1 个答案:

答案 0 :(得分:0)

你正在重新发明轮子......为什么你的轮子不起作用?

因为你在表上使用tostring,所以表上的tostring总是返回表名,而不是它的内容。

你想做什么,无论如何我在这里描述得太复杂了,但这里是你重新发明的轮子:

CoronaLabs.com "how to upload image to server"

:)