Hi fellow Corona devs,
I’m trying to send a .pdf / docx (binary) file which I have base64 encoded as part of the network.request() call.
Here is the code I’m using to test:
local fileName = "document.docx" local pathToImport = system.pathForFile( fileName, system.DocumentsDirectory ) local fileHandle = io.open(pathToImport,"rb") local mime = require "mime"; local encodedFile = mime.b64(fileHandle:read( "\*a" )); local body = "fileName=".. fileName .. "&encodedFile =" .. encodedFile local postParams = {} postParams.body = body network.request( "http://www.domain.com/file.php", "POST", uploadCompleted, postParams )
Within the php file I am base64_decoding the encoded string. I can see the decoded string present (since I output it for testing purposes). So all the data I expect to be available via the request is present.
Once I base64 decode the string I save it to disk. All appears to be working expected.
However when I try to view the saved docx file in Word, it complains that the file is corrupt or not of the right format. It’s also happening to .png images, .pdf files etc. Basically any binary data I sending across the network.
Has anybody experienced the same issue with regards to base64 encoding and the file becoming corrupted? If so I’d be very interested to know what you did about it.
I’ve read on some *older* (2013) forum posts that network.request has / had a bug where it corrupts encoded base64 values? Is this still true?
I’ve also seen this forum post: https://forums.coronalabs.com/topic/70098-help-how-to-covert-image-to-base64/
where the solution is to load a binary (image) file, save it as a binary file and reload it as a binary file. I tried something similar but that didn’t work. Or perhaps I did it incorrectly.
I’ve read that I could also use network.upload() but in the docs, it says that network.upload simply calls network.request eventually anyway. Perhaps I’m missing some request header info?
I’m really at a loss as to what I should try next. So any help would be greatly appreciated.
Thanks for reading.