mime.b64 Encoding - Uploading File Error

Hi there,

I’m trying to upload a file to a server using Corona. I’m using mime.b64 encoding, based on the example I saw here:

http://developer.coronalabs.com/code/upload-binary-corona-php-script

Instead of passing the file to a php script I’m using ASP.Net (VB.Net).

The problem I have is that I get the following error back from my VB.Net script, which looks like the encoding Corona-side has a problem and isn’t being performed correctly. The .Net script runs fine on its own.

_ “Invalid length for a Base-64 char array or string” _

My code is as follows:

LUA upload function:

function uploadBinary ( filename, url, onComplete )         local mime = require "mime"         local path = system.pathForFile( filename )         local fileHandle = io.open( path, "rb" )         if fileHandle then                 local params = {                         body = "png=" .. mime.b64( fileHandle:read( "\*a" )) .. "&filename="..filename                 }                 params.progress = "download";                 io.close( fileHandle )                 network.request( url, "POST", networkListener,  params)         end end -- Call the upload binary function uploadBinary ( "image.png", "http://myDomain.com/receiveFile.aspx")

LUA listener

function networkListener( event )         local phase = event.phase;         if (phase == "ended") then             if ( event.isError ) then                 print( "Network error!")             else                 print ( "RESPONSE: " ..event.response )             end         end end  

ASP.Net script to save file to server

Partial Class receiveFile     Inherits System.Web.UI.Page     Dim m\_strImageName As String     Dim m\_strPNGBinary As String     Public m\_response As String     Protected Sub Page\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load         m\_strImageName = Request("filename") 'file name         m\_strPNGBinary = Request("png") 'binary image details         If len(m\_strPNGBinary) \> 0 Then             Dim saveToFolder As String = "C:\inetpub\wwwroot\myDomain\http\" + m\_strImageName             Dim b() As Byte = Convert.FromBase64String(m\_strPNGBinary)             If File.Exists(saveToFolder) Then             File.Delete(saveToFolder)             End If             File.WriteAllBytes(saveToFolder, b)                          m\_response = "err=0"         Else                 m\_response = "err=1"         End if     End Sub End Class  

If anyone has any idea why I’m getting this error message back I’d be really grateful for any input. I’m totally stuck on this one.

_ “Invalid length for a Base-64 char array or string” _

I have tried changing from mime.b64 to use other methods such as those shown in this thread:

http://forums.coronalabs.com/topic/24714-fyi-base64-decode-and-encode-in-corona/

…and this one:

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

But I get exactly the same error whatever I try.

Thanks,

Ian

You might want to print out the base64 data on the server side before you try to decode it there might be an issue with line breaks or something else in the received data.

Thanks for the reply. Much appreciated.

There are line breaks, spaces, equals signs (=) and forward slash symbols (/) - the rest is numbers and letters.

Interestingly though, if I print it out in Corona before it’s sent the line breaks and spaces aren’t there. They are all plus (+) symbols.

So it seems to be replacing plus symbols with line breaks and spaces.

Will try and replace them back and see what happens.

Thanks.

Ian

Brilliant. That worked. Thanks for the suggestion.

I will post my code once it’s all cleaned up as there doesn’t seem to be a .Net example around.

Thanks,

Ian

Actually as far as I know there should be no simbols or spaces of any kind in base64 encoded string. There should only be two “=” symbols at the end. Something is changing the encoded string.

Sorry the number of “=” symbols differs they are there for 24bit alignment.

hmm it seems I was wrong. I checked and it appears “+” and “/” are possible as well. So you should be good just replacing the line breaks and spaces.

Got this working including a progress bar. Will post the code.

Thanks for the help.

Have posted the finished code here - http://forums.coronalabs.com/topic/44199-example-file-upload-using-aspnet-and-a-progress-bar/

You might want to print out the base64 data on the server side before you try to decode it there might be an issue with line breaks or something else in the received data.

Thanks for the reply. Much appreciated.

There are line breaks, spaces, equals signs (=) and forward slash symbols (/) - the rest is numbers and letters.

Interestingly though, if I print it out in Corona before it’s sent the line breaks and spaces aren’t there. They are all plus (+) symbols.

So it seems to be replacing plus symbols with line breaks and spaces.

Will try and replace them back and see what happens.

Thanks.

Ian

Brilliant. That worked. Thanks for the suggestion.

I will post my code once it’s all cleaned up as there doesn’t seem to be a .Net example around.

Thanks,

Ian

Actually as far as I know there should be no simbols or spaces of any kind in base64 encoded string. There should only be two “=” symbols at the end. Something is changing the encoded string.

Sorry the number of “=” symbols differs they are there for 24bit alignment.

hmm it seems I was wrong. I checked and it appears “+” and “/” are possible as well. So you should be good just replacing the line breaks and spaces.

Got this working including a progress bar. Will post the code.

Thanks for the help.

Have posted the finished code here - http://forums.coronalabs.com/topic/44199-example-file-upload-using-aspnet-and-a-progress-bar/