Skip to content

Instantly share code, notes, and snippets.

@johnnyferreiradev
Created May 10, 2022 21:54
Show Gist options
  • Save johnnyferreiradev/f4ee2ec0be8f2f4057d215026b5cda32 to your computer and use it in GitHub Desktop.
Save johnnyferreiradev/f4ee2ec0be8f2f4057d215026b5cda32 to your computer and use it in GitHub Desktop.
Convert base64 to File and add to input file
const dataURLtoFile = (dataurl, filename) => {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: mime });
};
const convertClick = (base64url, fileName) => {
var file = dataURLtoFile(base64url, fileName);
let container = new DataTransfer();
container.items.add(file);
document.querySelector('#input-file').files = container.files;
var newfile = document.querySelector('#mainImageInput').files[0];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment