Skip to content

Instantly share code, notes, and snippets.

@fasterv410
Last active March 9, 2024 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fasterv410/3f2a7a47ad4eed397b2f222dac873dad to your computer and use it in GitHub Desktop.
Save fasterv410/3f2a7a47ad4eed397b2f222dac873dad to your computer and use it in GitHub Desktop.
const base64ToFile (base64Data: string, filename: string, mimeType: string): File => {
const binaryString = atob(base64Data)
const arrayBuffer = new ArrayBuffer(binaryString.length)
const uint8Array = new Uint8Array(arrayBuffer)
for (let i = 0; i < binaryString.length; i++) {
uint8Array[i] = binaryString.charCodeAt(i)
}
const blob = new Blob([uint8Array], { type: mimeType })
return new File([blob], filename, { type: mimeType })
}
// base64ToFile(<BASE_64>, 'banner.png', 'image/png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment