Skip to content

Instantly share code, notes, and snippets.

@adeleke5140
Created November 2, 2023 19:55
Show Gist options
  • Save adeleke5140/823569544759d15687f84a2cfcbc5389 to your computer and use it in GitHub Desktop.
Save adeleke5140/823569544759d15687f84a2cfcbc5389 to your computer and use it in GitHub Desktop.
Conversion between js formats
const blob = b64toBlob(imageSrc.replace(/^data:image\/jpeg;base64,/, ""));
const file = new File([blob], "profile.jpeg", { type: "image/jpeg" });
const blobURl = URL.createObjectURL(file);
function convertDataUrlToFile(dataURL: string, filename: string) {
let arr = dataURL.split(","),
mime = arr[0]?.match(/:(.*?);/)![1],
bstr = atob(arr[arr.length - 1]!),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: mime });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment