I am using these function a plugin coming soon, thought they could be helpful for share files to another device with services like gamkit, autolan( it does technical have a file send service build in), or one signal. You also can store files inside your lua code. Of course these functions generate long strings so beware of size limits on some services.
local function encodeFile( filename,dir ) local base64 = require("base64") local myDir = system.ResourceDirectory if (dir) then myDir = dir end local path = system.pathForFile( filename, myDir ) local fileHandle = io.open( path, "rb" ) local tempString=base64.encode(fileHandle:read( "\*a" )) local tempTable = filename:split("%.") local tempTable2 = tempTable[1]:split("/") local tempString3 = "" if (tempTable2 and tempTable2[1]) then tempString3= tempTable2[#tempTable2] else tempString3= tempTable[1] end local tempString2 = tempString.."\_"..tempString3.."\_"..tempTable[2] return tempString2 end local function decodeFile( txt, dir ) local base64 = require("base64") local myDir = system.DocumentsDirectory if (dir) then myDir = dir end local tempTable= txt:split("\_") local tempString2 = "" for i=1,#tempTable-2 do tempString2 = tempString2..tempTable[i] end local path local file, errorString if (path) then path = system.pathForFile( path2.."/"..tempTable[#tempTable-1].."."..tempTable[#tempTable], myDir ) file, errorString= io.open( path, "w" ) else path = system.pathForFile( tempTable[#tempTable-1].."."..tempTable[#tempTable], myDir) file, errorString= io.open( path, "w" ) end if not file then else -- Write data to file file:write( base64.decode(tempString2) ) -- Close the file handle io.close( file ) end return tempTable[#tempTable-1].."."..tempTable[#tempTable], dir end
You also need to include this lua file
local mime=require('mime') local socket=require('socket') local base64 = {} function base64.encode(data) local len = data:len() local t = {} for i=1,len,384 do local n = math.min(384, len+1-i) if n \> 0 then local s = data:sub(i, i+n-1) local enc, \_ = mime.b64(s) t[#t+1] = enc end end return table.concat(t) end function base64.decode(data) local len = data:len() local t = {} for i=1,len,384 do local n = math.min(384, len+1-i) if n \> 0 then local s = data:sub(i, i+n-1) local dec, \_ = mime.unb64(s) t[#t+1] = dec end end return table.concat(t) end return base64