In this short tutorial you’ll see how to upload image with preview in bootstrap 5.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap 5 image Upload with Preview</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div class="container col-md-6">
<div class="mb-5">
<label for="Image" class="form-label">Bootstrap 5 image Upload with Preview</label>
<input class="form-control" type="file" id="formFile" onchange="preview()">
<button onclick="clearImage()" class="btn btn-primary mt-3">Click me</button>
</div>
<img id="frame" src="" class="img-fluid" />
</div>
<script>
function preview() {
frame.src = URL.createObjectURL(event.target.files[0]);
}
function clearImage() {
document.getElementById('formFile').value = null;
frame.src = "";
}
</script>
</body>
</html>