1

Im creating an app for uploading pictures to a wordpress site. I am using Ionic with capacitor to create the app for Ios and Android. Everything works great on Android. But i have an issue on Ios.

When i take the base64 data from the Camera plugin and try to convert it to a Blob with the Blob() constructor it returns an empty array as seen in the screenshot from a console.log.

Screenshot of console.log

The function that returns the blob is here (Inspired by this post):

const b64toBlob = (b64Data: string, contentType = 'image/jpeg', sliceSize = 512) => {
    const byteCharacters = atob(b64Data)
    const byteArrays = []
    for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        const slice = byteCharacters.slice(offset, offset + sliceSize)
        const byteNumbers = new Array(slice.length)
        for (let i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i)
        }
        const byteArray = new Uint8Array(byteNumbers)
        byteArrays.push(byteArray)
    }
    console.log('bytearrays',byteArrays)
    const blob = new Blob(byteArrays, { type: contentType })
    console.log('Blob:')
    console.log(blob)
    return blob
}
export default b64toBlob

I have tried searching for other similar problems but couldn't find any with the same issue.

  • you can also use "readAsArrayBuffer" method of "FileReader". First you need to convert your base64 in to File. then use FileReader object with "readAsArrayBuffer" method. – Ravi Ashara Nov 09 '22 at 06:26
  • @RaviAshara What do i do with the arrayBuffer after? If it is instead of the byteArrays i dont think it will be better. My issue i think, is with to Blob() Constructor and not in the first part of the function. – Jonathan Madsen Nov 11 '22 at 12:44

0 Answers0