Social network vk.com. Uploading files.

Hello  to all and especially Corona Staff!

I have spent a lot of time trying to upload photo to vk.com social network.

So, trouble in posting photo to users wall.

We should go through 4 steps to upload photo (http://vk.com/dev/upload_files?f=Uploading%20Photos%20on%20User%20Wall).

  1. Getting uploading server URL. This works

  2. Uploading photo to received server URL. Here is problem!

  3. Saving photo to wall album. This will work surely

  4. Posting to wall. This works (with message only, no photo)

Paragraph 2 in more details:

VK API says: 

The application forms  POST  request for the received URL.  

The request shall include  photo  field containing a file with an image (JPG, PNG, BMP or GIF) 

I tried standard Corona method:

network.upload( postData.uploadUrl, "POST", networkListener, postData.postFile, postData.postDir, "multipart/form-data" )

Not working, because API wait POST with parameter named “photo”. How to put parameter name to this request?

Ok, I tried to go another way:

local headers = {} headers["Content-Type"] = "multipart/form-data" local path = system.pathForFile( postData.postFile, postData.postDir ) local file = io.open( path, "r" ) local len = file:seek( "end", 0 ) local imageData = file:read( "\*a" ) io.close(file) headers["Content-Length"] = len local params = {} params.headers = headers params.body = "photo="..imageData --and also tried "photo="..path network.request( postData.uploadUrl, "POST", networkListener, params) 

And this also not works.

Extremely need help!!!

Thanks

Anybody have idea?

Try this approach ( didn’t test it )

 local mime = require "mime" local endpoint = "post\_url\_goes\_here" local function upload ( fileName, onComplete ) local params = {} params.headers["Content-Type"] = "multipart/text" local path = system.pathForFile( fileName, system.DocumentsDirectory ) local fileHandle = io.open( path, "rb" ) local data = mime.b64( fileHandle:read( "\*a" ) ) io.close( fileHandle ) params.body = data local function networkListener( event ) local response if ( event.isError ) then print( "Network error!") else print ( "RESPONSE: " .. event.response ) response = json.decode ( event.response ) end onComplete ( response ) end network.request( endpoint .. fileName, "POST", networkListener, params) end upload ( "your\_image.jpg", listener )

Thank you for reply!

I am at panic - this API functionality works on php, obj-c, java… But not on Corona!!

I have already tried to do like yours, but it is not work. I have also create simple php-script which should catch all POST parameters came in but it didn’t see any POST data…

\<?php $count = 0; foreach ($\_POST as $key =\> $value) { $count++; echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."\<br\>"; } echo "---\<br\>"; echo "Count ".(string)$count; ?\>

If you have some free time please read api description in the link above. API waiting image data in parameter named “photo”

Sorry for my english. 

Network upload does not do multi-part mime upload.  It basically dumps the data to the stream and doesn’t work with POST.  See:

http://coronalabs.com/blog/2014/02/25/tutorial-uploading-files-demystified/

Now if they want POST you’re probably going to need to use network.request() instead of network.upload().  You will construct your body like you’re doing in the second version, but you will likely need to base64 encode the image data since POST and binary don’t get along very well.  Depending on how the receiving script handles things, you may also may need to include some additional headers and such.

Rob

Thanks, Rob! Really good tutorial for network.upload()

I have contact to API developers, and they said their API will not accept base64 encoded image data.

As I have Enterprise version is it possible to create plugin in xCode and then include it to all my corona apps? 

And if it is possible, need some link…

Anybody have idea?

Try this approach ( didn’t test it )

 local mime = require "mime" local endpoint = "post\_url\_goes\_here" local function upload ( fileName, onComplete ) local params = {} params.headers["Content-Type"] = "multipart/text" local path = system.pathForFile( fileName, system.DocumentsDirectory ) local fileHandle = io.open( path, "rb" ) local data = mime.b64( fileHandle:read( "\*a" ) ) io.close( fileHandle ) params.body = data local function networkListener( event ) local response if ( event.isError ) then print( "Network error!") else print ( "RESPONSE: " .. event.response ) response = json.decode ( event.response ) end onComplete ( response ) end network.request( endpoint .. fileName, "POST", networkListener, params) end upload ( "your\_image.jpg", listener )

Thank you for reply!

I am at panic - this API functionality works on php, obj-c, java… But not on Corona!!

I have already tried to do like yours, but it is not work. I have also create simple php-script which should catch all POST parameters came in but it didn’t see any POST data…

\<?php $count = 0; foreach ($\_POST as $key =\> $value) { $count++; echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."\<br\>"; } echo "---\<br\>"; echo "Count ".(string)$count; ?\>

If you have some free time please read api description in the link above. API waiting image data in parameter named “photo”

Sorry for my english. 

Network upload does not do multi-part mime upload.  It basically dumps the data to the stream and doesn’t work with POST.  See:

http://coronalabs.com/blog/2014/02/25/tutorial-uploading-files-demystified/

Now if they want POST you’re probably going to need to use network.request() instead of network.upload().  You will construct your body like you’re doing in the second version, but you will likely need to base64 encode the image data since POST and binary don’t get along very well.  Depending on how the receiving script handles things, you may also may need to include some additional headers and such.

Rob

Thanks, Rob! Really good tutorial for network.upload()

I have contact to API developers, and they said their API will not accept base64 encoded image data.

As I have Enterprise version is it possible to create plugin in xCode and then include it to all my corona apps? 

And if it is possible, need some link…