gistlib
To create a file preview in JavaScript, you can follow the below steps:
<form> <input type="file" id="fileInput"> </form> 52 chars4 lines
<form> <input type="file" id="fileInput"> </form>
index.tsxconst fileInput = document.getElementById('fileInput'); fileInput.addEventListener('change', (event) => { const file = event.target.files[0]; // do something with the file }); 181 chars7 lines
const fileInput = document.getElementById('fileInput'); fileInput.addEventListener('change', (event) => { const file = event.target.files[0]; // do something with the file });
index.tsxconst fileInput = document.getElementById('fileInput'); const previewContainer = document.getElementById('previewContainer'); const previewImage = previewContainer.querySelector('.preview-image'); fileInput.addEventListener('change', (event) => { const file = event.target.files[0]; const reader = new FileReader(); reader.addEventListener('load', () => { previewImage.setAttribute('src', reader.result); }); reader.readAsDataURL(file); }); 458 chars15 lines
const fileInput = document.getElementById('fileInput'); const previewContainer = document.getElementById('previewContainer'); const previewImage = previewContainer.querySelector('.preview-image'); fileInput.addEventListener('change', (event) => { const file = event.target.files[0]; const reader = new FileReader(); reader.addEventListener('load', () => { previewImage.setAttribute('src', reader.result); }); reader.readAsDataURL(file); });
.preview-container { width: 50%; margin: 0 auto; text-align: center; } .preview-image { max-width: 100%; max-height: 300px; margin-top: 20px; } 157 chars12 lines
.preview-container { width: 50%; margin: 0 auto; text-align: center; } .preview-image { max-width: 100%; max-height: 300px; margin-top: 20px; }
gistlibby LogSnag