<input type="file" accept="image/*"/>
<img class="img-fluid applybr" id="sample_img" src="/images/sample.jpg">
<script>
$(function () {
$('input[type=file]').change(function(){
let reader = new FileReader();
if ($(this).val() != '') {
if(!(/(?:jpg|gif|png|jpeg|webp)$/i.test($(this).val()))){
alert("只允許上傳圖片");
return false;
}
reader.onload = (e) => {
$('#sample_img').attr('src', e.target.result);
}
reader.readAsDataURL(this.files[0]);
}
});
});
</script>