This entry is inspired by this post
Today I was working on a plugin that uploads an array of files to the bubble server, and I ran into some problems due to a lack of documentation about context.uploadContent() (and if it does exist, it’s so burried, I couldn’t find it …)
Problem:
context.uploadContent(fileName, base64String, callbackFunction);
was not working for me. The files would show up in the file manager, however they were corrupted:
And when you would click on it, this page would appear
Turns out, the problem is the way bubble requires the base64 encoded string to be given to the server is weird (keep in mind, I am somewhat a beginner, so this may be obvious to more advanced coders).
What NOT to do:
"data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlb ..."
This is what a normal base64 encoded string looks like. Giving this will get uploaded successfully, but it will cause the corruption problem we saw before.
The RIGHT way:
"PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTg ..."
For an unknown reason, you must remove the first two parts of the string:
data:image/svg+xml;base64,
Leaving this information in the string will cause the problem we saw. Thankfully, the solution is simple: use the split(",")
function! It is the key to solving the issue.
Example:
const temp = base64String.split(',') //Splits up the b64 string into pieces const data = tmp[1] //Takes only the part of the string we need console.log(data); //Double check if it gets formatted correctly
Then,
context.uploadContent(fileName, `${data}`, callbackFunction)
After all of this, your files should get uploaded correctly
I hope this documentation was helpful for someone out there. Once again you can check the original poster’s post who found the solution here