1
Good night! I’m doing an image/video preview before the user posts it, I’m wanting to do it with a single input and would let JS check the file and return the extension so I can display the desired preview... I did this with the image and it worked, but when I upload a video instead of the image, the image preview (without any image) and the video preview with it running.
<!-- Div de preview da imagem -->
<div class="imagem_prepost">
<a class="excluir-foto_prepost" id="excluir-foto_prepost"><i class="fas fa-times"></i></a>
<!-- preview da img -->
<img class="img_preview" alt="">
<!-- preview video -->
<video class="vid_preview" autoplay muted loop></video>
</div>
<!-- input que receberá video ou imagem -->
<input name="arquivo" accept="video/mp4" id="input_preview" type="file" />
<script>
$(function(){
$('#input_preview').change(function(){
const file = $(this)[0].files[0]
const fileReader = new FileReader()
// Se algum arquivo foi passado
if (file) {
$('.img_preview').show();
$('#excluir-foto_prepost').show();
//Excluindo a foto caso não queira postar
fileReader.readAsDataURL(file);
$("#excluir-foto_prepost").click(function(){
$('.img_preview').hide();
$('#excluir-foto_prepost').hide();
$('#input_preview').val(null);
});
}
// Se não foi passado nenhum arquivo
else if(!file){
$('.img_preview').hide();
$('#excluir-foto_prepost').hide();
}
fileReader.onloadend = function(){
$('.img_preview').attr('src',fileReader.result);
}
})
})
</script>
good night buddy!! I tested here by running and only the image is working... you have any idea what it would be like?
– Filipe Oliveira
I put only these video extensions in the example: 'mp4', 'mpeg', 'wmv' ; you are probably testing another, just add in the array. It is just below the comment /video : var fileExtension_vid = ['mp4', 'mpeg', 'wmv'];
– Leonardo Negrão