<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <button id="btn" onclick="openBrowse()">上传文件</button> <input type="file" id="file" style="display:none"> <script> function openBrowse() { var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false; if (ie) { document.getElementById("file").click(); // document.getElementById("filename").value = document.getElementById("file").value; } else { var a = document.createEvent("MouseEvents");//FF的处理 a.initEvent("click", true, true); document.getElementById("file").dispatchEvent(a); } } (function handleFiles() { var vm = this; var fileInput = document.querySelector(‘#file‘); fileInput.onchange = function () { var file = this.files[0]; var file_typename = file.name.substring( file.name.lastIndexOf(‘.‘) + 1, file.name.length); if(file_typename!=‘mp3‘){ alert(‘请上传MP3文件‘); return } vm.mp3FileName=file.name; var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function () { console.log(reader.result) }; } })() </script> </body> </html>
原文地址:https://www.cnblogs.com/obh3274/p/8947658.html
时间: 2024-11-02 21:18:59