Upload photo from device camera

Hi friends,

I’m trying to upload a photo taken with the device camera to a remote server. I’m using the “How To - Upload an Image to a server (multipart/form-data)” example that you can find here:

http://developer.coronalabs.com/code/how-upload-image-server-multipartform-data

So far, I was able to successfully upload a local image to the remote system. However I’m unsuccessfully when trying to use the camera output.

Here is the my code:

[lua] local sessionComplete = function(event)
local image = event.target

local MultipartFormData = require(“class_MultipartFormData”)

local multipart = MultipartFormData.new()
multipart:addFile(“myfile”, system.pathForFile(event.target), “image/jpeg”, “myfile.jpg”)

local params = {}
params.body = multipart:getBody() – Must call getBody() first!
params.headers = multipart:getHeaders() – Headers not valid until getBody() is called.

local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
else
print ( tostring( image ) … " RESPONSE: " … event.response )
end
end

network.request( “http://myserver.com/uploader64.php”, “POST”, networkListener, params)
end

local onButtonRelease = function( event )
if media.hasSource( media.Camera ) then
media.show( media.Camera, sessionComplete)
else
native.showAlert(“Corona”, “Camera not found.”)
end
return true
end[/lua]

Now my doubt is on the following line of code:
[lua]multipart:addFile(“myfile”, system.pathForFile(event.target), “image/jpeg”, “myfile.jpg”)[/lua]

The part where I have system.pathForFile(event.target) is that correct? This code is sending a zero bytes file. I’ve only tried this with corona simulator.

Any help here is welcomed.

Thank you [import]uid: 189638 topic_id: 33231 reply_id: 333231[/import]

I would follow the example more closely. Instead of using system.pathForFile(event.target) why not save the image locally, and then refer to that file & location?

local onComplete = function(event)  
  
local photo = event.target  
local photoGroup = display.newGroup()   
photoGroup:insert(photo)  
  
local myDirectory = system.TemporaryDirectory --or system.DocumentsDirectory  
display.save(photoGroup:insert, "myPhotoName.jpg", myDirectory)   
  
--then do your multipart data stuff using:  
multipart:addFile("myFile", system.pathForFile( "myPhotoName.jpg", myDirectory ), "image/jpeg", "myPhotoName.jpg")  
  

It’s an extra step, but it does at least work. This is assuming of course that your server side code is all correct. [import]uid: 84115 topic_id: 33231 reply_id: 132068[/import]

I use this in my app but also do the same way as AlanPlantPot suggests.

Dave [import]uid: 117617 topic_id: 33231 reply_id: 132071[/import]

Alan and Dave thanks for the feedback.

That display.save what it does is like a screenshot right? You put the photo on the device screen and then do a screenshot. The problem is that I need to preserve the photo EXIF info. Specifically I need the GPS location.

Thanks again [import]uid: 189638 topic_id: 33231 reply_id: 132088[/import]

Have you tried changing -

multipart:addFile("myfile", system.pathForFile(event.target), "image/jpeg", "myfile.jpg")  

to -

multipart:addFile("myfile", image, "image/jpeg", "myfile.jpg")  

Dave [import]uid: 117617 topic_id: 33231 reply_id: 132103[/import]

Dave,

Yes I’ve tried that already and no joy :frowning:

I’ve just build the app for device. I only have an 3gen iPad to test it, and the camera interface doesn’t even open! On the corona simulator the camera is working.

Am I doing something wrong?

Thanks [import]uid: 189638 topic_id: 33231 reply_id: 132156[/import]

All looks fine from what you have shown us, obviously we haven’t seen the rest of your code.

Dave [import]uid: 117617 topic_id: 33231 reply_id: 132161[/import]

I would follow the example more closely. Instead of using system.pathForFile(event.target) why not save the image locally, and then refer to that file & location?

local onComplete = function(event)  
  
local photo = event.target  
local photoGroup = display.newGroup()   
photoGroup:insert(photo)  
  
local myDirectory = system.TemporaryDirectory --or system.DocumentsDirectory  
display.save(photoGroup:insert, "myPhotoName.jpg", myDirectory)   
  
--then do your multipart data stuff using:  
multipart:addFile("myFile", system.pathForFile( "myPhotoName.jpg", myDirectory ), "image/jpeg", "myPhotoName.jpg")  
  

It’s an extra step, but it does at least work. This is assuming of course that your server side code is all correct. [import]uid: 84115 topic_id: 33231 reply_id: 132068[/import]

I use this in my app but also do the same way as AlanPlantPot suggests.

Dave [import]uid: 117617 topic_id: 33231 reply_id: 132071[/import]

Alan and Dave thanks for the feedback.

That display.save what it does is like a screenshot right? You put the photo on the device screen and then do a screenshot. The problem is that I need to preserve the photo EXIF info. Specifically I need the GPS location.

Thanks again [import]uid: 189638 topic_id: 33231 reply_id: 132088[/import]

Have you tried changing -

multipart:addFile("myfile", system.pathForFile(event.target), "image/jpeg", "myfile.jpg")  

to -

multipart:addFile("myfile", image, "image/jpeg", "myfile.jpg")  

Dave [import]uid: 117617 topic_id: 33231 reply_id: 132103[/import]

Dave,

Yes I’ve tried that already and no joy :frowning:

I’ve just build the app for device. I only have an 3gen iPad to test it, and the camera interface doesn’t even open! On the corona simulator the camera is working.

Am I doing something wrong?

Thanks [import]uid: 189638 topic_id: 33231 reply_id: 132156[/import]

All looks fine from what you have shown us, obviously we haven’t seen the rest of your code.

Dave [import]uid: 117617 topic_id: 33231 reply_id: 132161[/import]

It is pretty difficult to get the code correct…Still you tried and it looks fine!

business phone providers [import]uid: 199065 topic_id: 33231 reply_id: 132382[/import]

It is pretty difficult to get the code correct…Still you tried and it looks fine!

business phone providers [import]uid: 199065 topic_id: 33231 reply_id: 132382[/import]