Reputation: 1928
I have a little problem related to uploading an image to server taken with the camera or from the photo album. I am using the following code to upload the image to my server: But with this code my server only recive the table value of the image like so: table: 0x11791daf0
, how can I get it to upload the image instead of the value? and would it also be possible to upload text with this code?
and the PHP script on my server in the end.
Thank you in advance :)
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); ?
Upvotes: 1
Views: 2146
Reputation: 6296
You are reinventing the wheel... And why your wheel don't work?
Because you are using tostring on a table, tostring on a table always returns the table name, not its contents.
What you want to do, is anyway too complex to me describe here, but here is the wheel you was reinventing:
CoronaLabs.com "how to upload image to server"
:)
Upvotes: 0