上传图片
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form> 文件: <input type="file" id="myFile" name ='myfile' onchange="getFileContent();"> <br> <input type="submit"> </form> <img src="" alt=""> <script> function getFileContent(){ var reader = new FileReader(); var file = document.querySelector('#myFile').files; console.log(file); reader.readAsDataURL(file[0]) reader.onload = function(){ console.log(123); document.querySelector('img').src = reader.result } } </script> </body> </html>
Sometimes you need to applaud yourself