Skip to content

Instantly share code, notes, and snippets.

@heldonjose
Last active May 11, 2022 00:34
Show Gist options
  • Save heldonjose/968752aef8d09b8996868fd140aede09 to your computer and use it in GitHub Desktop.
Save heldonjose/968752aef8d09b8996868fd140aede09 to your computer and use it in GitHub Desktop.
Converter base64 to inputFile
#https://stackoverflow.com/questions/71466930/how-to-get-base64-img-src-and-insert-it-into-input-files
function 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});
}
function convertBaseToInputFile(base64url, idInputFile, fileNameExtension) {
var file = dataURLtoFile(base64url, fileNameExtension);
let container = new DataTransfer();
container.items.add(file);
document.querySelector(`#${idInputFile}`).files = container.files;
}
#Usando
convertBaseToInputFile(base64, 'id_inputFile', "namefile.png")
#pegar o file por JS
var newfile = document.querySelector('#id....').files[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment