When sending data from your client to Firebase, it gets serialized into a JSON object. This means you're limited to storing types that can be represented with JSON primitives:
- Strings (Unicode)
- Booleans
- Numbers (64 bit floating point)
null
If your browser supports the File API and you can convert a file to one of the above types, then yes, you can store it in Firebase (as long as it's under 10mb).
For files encoded with Unicode, simply read them into string variables and pass them to Firebase like any other string.
Working with binary files is more complex.
Consider images. You can use the readAsDataURL
method to get a base64 encoded string which can be serialized to JSON for storage in Firebase. When retrieving the image data, other users can display it using the src='data:image/png;base64,
syntax.
An example project utilizing this technique can be found here.
A more intricate case would involve a binary file format such as Midi. While you can read the file into a base64 string, browsers cannot play it directly, so users will need to download it. Providing a base64 string won't work either, as most Midi players are unable to interpret it.
To address this, you'll have to create a decoder to convert the base64 data back into binary before saving it to file or offering it for download. This process usually requires converting it into a blob using typed arrays.