user6822927
user6822927

Reputation:

How To Upload An Image using Corona SDK Lua

I am creating a social app with PHP and corona sdk (Lua)(something like Instagram) . One of the option that the user has is to upload a picture from their phone and use it as a profile picture on their account .

I have no idea how to upload a picture to a folder inside my folder where the app is and then show that picture on the users profile . I've looked at tutorials but they didn't help me. Can someone please help ?

Upvotes: 1

Views: 809

Answers (1)

Usman Mughal
Usman Mughal

Reputation: 211

You question description is too wide. Be specific about your question. I imagine you are asking to upload image from Corona SDK as your question title.

Here is image uploading to server from Corona SDK.

1 - Image selection from Gallery.

-- Selection completion listener
local function onComplete( event )
    local photo = event.target

    if photo then
        print( "photo w,h = " .. photo.width .. "," .. photo.height )
    end
end

local button = display.newRect( 120, 240, 80, 70 )

local function pickPhoto( event )

    media.selectPhoto(
    {
        mediaSource = media.SavedPhotosAlbum,
        listener = onComplete, 
        origin = button.contentBounds, 
        permittedArrowDirections = { "right" },
        destination = { baseDir=system.TemporaryDirectory, filename="image.jpg" } 
    })
end

button:addEventListener( "tap", pickPhoto )

Corona doc for media.selectPhoto

2 - Uploading image to server.

You need MultipartFormData library added into your project. Here is Link

Then

local MultipartFormData = require("class_MultipartFormData")
local multipart = MultipartFormData.new()
local path=system.pathForFile( "image.jpg", system.TemporaryDirectory )
multipart:addFile("Image", path, "image/jpg", "image.jpg")

local params = {}        
params.body = multipart:getBody()
params.headers = multipart:getHeaders() -- Headers not valid until getBody() is called.

network.request("https://your.server.url/services/imageupload?parameter=1", "POST", listener, params)

You can pass parameters to url too after a ?. Check the url.

Upvotes: 1

Related Questions