Share excellent template plug-in resources
Current position: home page > Website Design >Upload the selected file Preview file front-end js code (Preview file javascript code)

Upload the selected file Preview file front-end js code (Preview file javascript code)

Author: blueleaf Classification: Website Design Time: January 22, 2024 Browse: 366

In the front end world, we sometimes need a magical experience that allows users to preview their content before uploading files. No, I brought you this piece of code. It is like a magic mirror, allowing you to preview music, pictures and video files. Simply copy the following javascript Code, paste it where you need it, and then the magic mirror will work for you. Please note that this is just a demonstration example, and you can modify and optimize it according to your own needs. Now, let's unveil its mystery together!

 Upload the selected file preview file front end js code 1.jpg

 <! doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,  initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"> <title>Select File Preview File Content</title> <style> *{margin:0; border:0;padding:0; box-sizing:border-box} .wrap{width: 600px; margin: 60px auto 0;background: #fff;border: 1px solid #ccc;padding: 10px;border-radius: 6px;} .wrap input{width:100%;} #viewbox{width:600px; margin:10px auto 0;} #viewbox img,#viewbox audio,#viewbox video{width:100%;} </style> </head> <body> <div class="wrap"> <input id="file" type="file" accept="image/*, video/*,audio/*" onChange="handleChange()" /> </div> <div id="viewbox"></div> <script type="text/javascript"> function handleChange() { var file = document.getElementById("file").files[0],  fileType = file.type, read = new FileReader(); document.getElementById("viewbox").innerHTML=''; read.readAsDataURL(file); read.onload = function() {		   var url = read.result; if(fileType.indexOf("audio/") != - 1){ var showdata = new Audio();     showdata.controls = true; }else if(fileType.indexOf("image/") != - 1){ var showdata = new Image(); }else if(fileType.indexOf("video/") != - 1){ var showdata = document.createElement("video"); showdata.controls = true; } showdata.src = url; document.getElementById("viewbox").appendChild(showdata); }; } </script> </body> </html>

Disclaimer:

Website: Click Copy to share with friends!

All content resources shared by this website, if not noted, are original by Blueleaf. If you need to reprint, please indicate the source; Please contact us in case of infringement Contact handling Please understand!

Comment List
 blink the eyes kiss Growl happy think poor It's embarrassing Grievance ha-ha hush Right hum Zuo Hum Hum doubt A bad laugh Make money Sadness Be cool Seduce fierce handshake yeah Hee hee Shyness applause greedy Madness Embrace gather and watch Mighty awesome
Submit comments

Clear information
Close Comment