当前位置:   article > 正文

HTML5实现头像的上传_html设置上传头像及回显

html设置上传头像及回显

HTML5实现头像的上传

这是利用form-data给后台传输数据,来实现头像的上传加载!

  1. html代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    </head>
<body>
    <div class="container">
        <input enctype="multipart/form-data" type="file">
        <input id="Susername"  type="text" placeholder="用户名">
        <input id="Ssex" type="number" placeholder="年龄">
        <input id="Sage" type="text" placeholder="专业">
        <input id="Smajor" type="text" placeholder="学校">
        <input id="Sschool" type="text" placeholder="QQ">
        <input id="Sqq" type="text" placeholder="地址">
        <input id="Saddress" type="text" placeholder="座右铭">
        <input id="Smotto" type="button" value="上传">
       
        <div style="clear: both;"></div>
             <progress value="0" max="100"></progress>
    </div>
    <div class="showarea">
        <h3>显示区域</h3>
    </div>
    </body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  1. 样式代码
 .container{
            box-sizing: border-box;
            width: 404px;
            height: 100px;
            border: 1px solid #ccc;
            border-radius: 5px;
            padding-top: 20px;
            background: linear-gradient(to bottom,#0ff,#0ff 20px,transparent 0);
            margin: 0 auto;
        }
        input{
            padding: 0;
            margin: 0;
          
        }
        .container input[type=file]{
            width: 300px;
            height: 30px;
            border: 1px solid #ccc;
            background: #7FFFD4;
            color: #133131;
            float: left;
        }
        .container input[typr=button]{
            width: 100px;
            height: 32px;
            float: left;
            border: 1px solid #ccc;
            color:  #133131;
        }
        progress{
            display: none;
            width: 400px;
            height: 30px;
            margin-top: 7px;
        }
        .showarea{
            width: 600px;
            min-height: 200px;
            border: 1px solid #ccc;
            margin: 30px auto;
        }
        .showarea h3{
            widows: 100px;
            margin: 0 auto;
            line-height: 60px;
            text-align: center;
            border-bottom: 1px solid #cccc;
            color: #133131;
            
        }
        .showareaimg{
            max-width: 1000%;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  1. javescript代码
  var file = document.querySelector('[type=file]');
        var sub = document.querySelector('[type=button]');
        var show = document.querySelector('.showarea');
        var progress = document.querySelector('progress');
        var Susername = document.getElementById('Susername');
        var Ssex = document.getElementById('Ssex');
        var Sage = document.getElementById('Sage');
        var Smajor = document.getElementById('Smajor');
        var Sschool = document.getElementById('Sschool');
        var Sqq = document.getElementById('Sqq');
        var Saddress = document.getElementById('Saddress');
        var Smotto = document.getElementById('Smotto');
        
        sub.onclick = function(e){
            var fileobje = file.files[0];
            var formdata = new FormData();
            formdata.append('upload',fileobje);
            formdata.append('upload',Susername.value);
            formdata.append('upload',Ssex.value);
            formdata.append('upload',Sage.value);
            formdata.append('upload',Smajor.value);
            formdata.append('upload',Sschool.value);
            formdata.append('upload',Sqq.value);
            formdata.append('upload',Saddress.value);
            formdata.append('upload',Smotto.value);
            var xhr = new XMLHttpRequest();
            var fr = new FileReader();
            fr.readAsDataURL(fileobje)
            fr.onload=function(e){
                // console.log(e);
                var img = document.createElement('img');
                img.src = this.result;
                // console.log( img.src);
                show.appendChild(img)
            }
            xhr.upload.onprogress = function(e){
                progress.value= parseInt(e.loaded/e.total*100)
            }
          xhr.open("POST","http://127.0.0.1:3000/avatar")
        //   console.log(formdata)
          xhr.send(formdata);
          console.log(formdata)
        console.log(formdata.getAll("upload"))
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/624830
推荐阅读
相关标签
  

闽ICP备14008679号