0
I needed to make an upload system, which when selecting the image, a preview would be shown in a box before it was sent. I saw a code here at Stackoverflow and I tried to adapt it. I would like to know if he is semantically correct, according to the language norms, and also, I would like an explanation about the code. (manjo de JQUERY/JS).
I thank everyone who helps :) Have a good weekend
$('.arquivo').change(function(){
var preview = $('.enviar_screenshot button'); //BOTAO DE SELECIONAR ARQUIVO
var file = $('.arquivo')[0].files[0]; // O ARQUIVO
var reader = new FileReader();
var ext = ['jpg','jpeg','png']; // EXTENSÕES PERMITIDAS
var extArquivo = file.name.split(".").pop(); //PEGA A EXTENSÃO DO ARQUIVO
reader.onloadend = function () {
if(typeof ext.find(function(extt){return extArquivo == extt; }) == 'undefined') { // VERIFICA SE A EXTENSÃO NÃO É VÁLIDA, SE NÃO, REALIZA O PROCESSO NORMAL
$(preview).css({'background-image':'none', 'opacity':'1'});
$(preview).html('<i class="fas fa-plus"></i>');
} else {
$(preview).css({'background-image':'url(' + reader.result + ')', 'background-size':'cover', 'background-position':'center', 'opacity':'0.5'});
}
}
if (file) { // MOSTRA
reader.readAsDataURL(file);
$(preview).text("");
} else {
$(preview).css({'background-image':'none', 'opacity':'1'});
$(preview).html('<i class="fas fa-plus"></i>');
}
});
The backend is already ready and working, I just want to know if the code was correct even, because I don’t know much. But thank you :)
– Vitor Leite