I've already read all other answers related to this. But it doesn't work for me or I just don't understand something. Please help.
I'm sending the 'image/png' base64 string from the client. It is something like this: "iVBORw0KGgoAAAANSUhEUgAAAgAAAA......."
On Cloud Functions I have the method:
exports.uploadImage = functions.https.onCall((data, context) => {
var bytes = data.imageData;
uploadFile(bytes);
})
function _base64ToArrayBuffer(binary_string) {
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return new Uint8Array(bytes);
}
async function uploadFile(data) {
const imageBuffer = _base64ToArrayBuffer(data)
const fielName = 'img/' + UTIL.getRandomString(20) + '.png'
const fileUpload = bucket.file(fielName)
const uploadStream = fileUpload.createWriteStream({
metadata: {
contentType: 'image/png'
}
});
uploadStream.on('error', (err) => {
console.log(err);
return;
});
uploadStream.on('finish', () => {
console.log('Upload success');
bucket.file(fielName).makePublic();
});
uploadStream.end(imageBuffer);
}
As a result I have some strange file saved on Firebase Storage that is 35Kb (the source image is 14kb)
The the image of this file not showing and there is no the link to the image created.
What can be wrong?