Skip to content

Instantly share code, notes, and snippets.

@anisur2805
Created February 10, 2024 15:18
Show Gist options
  • Save anisur2805/beee64aec5831056f0d941c43fdb4c58 to your computer and use it in GitHub Desktop.
Save anisur2805/beee64aec5831056f0d941c43fdb4c58 to your computer and use it in GitHub Desktop.
Upload image and preview image
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Upload and Preview Image</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input
type="file"
name="file"
accept="image/*"
onchange="previewFile(event)"
/>
<img src="" id="preview" style="max-width: 290px; height: auto;" />
</form>
<script>
function previewFile(event) {
var input = event.target;
var reader = new FileReader();
reader.onload = function(){
var dataUrl = reader.result
var output = document.getElementById('preview');
output.src = dataUrl;
console.log({dataUrl})
}
reader.readAsDataURL(input.files[0]);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment