Created
May 10, 2022 21:54
-
-
Save johnnyferreiradev/f4ee2ec0be8f2f4057d215026b5cda32 to your computer and use it in GitHub Desktop.
Convert base64 to File and add to input file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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