H5调用手机拍照并展示在前端页面
<!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>手机拍照</title> <style> #showimg{ width: 150px; height: 150px; } #file_input{ opacity: 0; position: absolute; width: 100%; height: 100; } #box{ position: relative; background: green; } </style> </head> <body> <div class="file-box" id="box"> 拍照上传 <input type="file" accept="image/*" capture="camera" id="file_input" class="file-btn"> </div> <img id="showimg"> </body> </html> <script> var showimg = document.getElementById("showimg"); var imginput = document.getElementById("file_input"); document.getElementById("box").onclick = function () { imginput.onchange = function () { var files = this.files; console.log(files) //本地的临时路径
// var formdata = new FormData();
//formdata.append('fileStream', files);
// formdata再传给后台
var url = URL.createObjectURL(files[0]); showimg.src = url; } } </script>