<!DOCTYPE html> {% load static %} {% get_static_prefix as getstatic %} <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta content="width=device-width, initial-scale=1"> <link href="https://www.jd.com/favicon.ico"> <link href="{{ getstatic }}plugins/bootstrap-3.3.7/css/bootstrap.min.css"> <title>注册页面</title> <style> .title-top { padding: 0; font-size: 21px; margin-top: 40px; } .form-padding { padding-right: 65px; } .has-feedback .country-code { width: 53px; text-align: right; padding-right: 5px; } .mobile-control-wrap { display: flex; } .mobile-num { margin-left: 10px; } .form-horizontal .form-group { margin-right: -15px; margin-left: -15px; } .body-box { margin-top: 70px; height: 567px; width: 68%; } .cnblogs-btn-blue { color: white; padding: 6px 14px; letter-spacing: 1em; padding-left: 2em; background-color: RGB(70, 136, 214); text-align: center; } .cnblogs-btn-blue :hover { color: white; } .navbar { background-color: RGB(40, 62, 92); } .side-img-box { height: 300px; position: relative; padding-left: 0; margin-top: 168px; } .side-img-box img { position: relative; width: 100%; top: 50%; transform: translateY(-50%); border-radius: 8px; } .text-danger { float: right; } .img-thumbnail { width: 100px; height: 100px; margin-bottom: 20px; margin-top: -10px; } #avatr_img { margin-left: 237px; } </style> </head> <body> <nav> <div> <!-- Brand and toggle get grouped for better mobile display --> <div> <img src="{{ getstatic }}img/TIM截图20180205110238.png"> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div> <ul> <li><a href="#">首页</a></li> <li><a href="#">登录</a></li> <li><a href="#">注册</a></li> <li><a href="#">帮助</a></li> </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> <div> <div> <!--头部提示--> <div> 注册新用户 <hr> </div> <div> <form role="form" method="post" novalidate="novalidate"> <!--每一行--> <div> <label for="avatar"> <img src="{{ getstatic }}img/registersideimg.png" alt="用户头像"> 头像 </label> <input type="file"> </div> <div> <div> <label for="Email">邮 箱</label> </div> <div> {{ register_form.email }} <span></span> </div> </div> <div> <div> <label for="DisplayName">昵称</label> </div> <div> {{ register_form.nik_name }} <span><span></span></span> </div> </div> <div> <div> <label for="Password">密 码</label> </div> <div> {{ register_form.password }} <span></span> </div> </div> <div> <div> <label for="ConfirmPassword">确认密码</label> </div> <div> {{ register_form.repeta_pwd }} <span><span></span></span> </div> </div> <!--注册按钮--> <div> <div> <span></span> <input type="button" value="登录"> </div> </div> <!--协议提示--> <div> <span>*</span> “注册” 按钮,即表示您同意并愿意遵守 <a target="_blank" href="https://passport.cnblogs.com/agreement.html">用户协议</a>。 </div> <!--hidden--> {% csrf_token %} </form> <div> <div> <div></div> </div> <img src="{{ getstatic }}img/registersideimg.png"> </div> </div> </div> </div> <script src="{{ getstatic }}js/jquery-3.2.1.min.js"></script> <script src="{{ getstatic }}plugins/bootstrap-3.3.7/js/bootstrap.min.js"></script> </body> </html>
上面的代码只是HTML ,而JavaScript在下面实例中
<script> $("#avatar").change(function () { var chooice_file = $(this)[0].files[0]; var reader = new FileReader();// 注意这里,预览本地文件 reader.readAsDataURL(chooice_file); reader.onload=function () { $("#avatar_img").attr("src",this.result) } }) function regesite() { var formdata = new FormData(); //z注意这里 涉及文件上传 formdata.append("email",$("#id_email").val()); formdata.append("nik_name",$("#id_nik_name").val()); formdata.append("password",$("#id_password").val()); formdata.append("repeta_pwd",$("#id_repeta_pwd").val()); formdata.append("valid_img",$("#avatar")[0].files[0]); formdata.append("csrfmiddlewaretoken",$("[name='csrfmiddlewaretoken']").val()); $.ajax({ url: "{% url "regeste" %}", type: "POST", data:formdata, contentType:false, processData:false, success: function (data) { response_data = JSON.parse(data) if(response_data.user){ location.href="{% url " login" %}" }else { // 清空上次错误信息 $("form span").html("") $(".has-feedback").removeClass("has-error") $.each(response_data.error_msg,function (fiel, error_info) { // 显示错误信息 $("#id_"+fiel).parent().addClass("has-error"); $("#id_"+fiel).next().html(error_info[0]);//显示全局错误 // 注意这里 if(fiel=="__all__"){ $("#id_repeta_pwd").next().html(error_info[0]).css("color","red") } }) } } }) } </script>
FileReader
FileReader主要用于将文件内容读入内存,通过一系列异步接口,可以在主线程中访问本地文件。