angular +H5 上传图片 与预览图片

//index.html
<form class="form-horizontal">    <div class="panel panel-default">        <div class="panel-body">            <div class="container-fluid">                <div class="row">                    <div class="form-group">                        <label class="col-sm-2 control-label">司机姓名<span class="need">*</span></label>                        <div class="col-sm-4">                            <input type="text" class="form-control" ng-model="name" maxlength="20">                        </div>                        <label class="col-sm-2 control-label">司机身份证号</label>                        <div class="col-sm-4">                            <input type="text" class="form-control" ng-model="idNo" maxlength="18">                        </div>                    </div>                    <div class="form-group">                        <label class="col-sm-2 control-label">手机号<span class="need">*</span></label>                        <div class="col-sm-4">                            <input type="text" class="form-control" ng-model="mobile" maxlength="11">                        </div>                    </div>                    <div class="form-group">                        <label class="col-sm-2 control-label">身份证正面</label>                        <div class="col-sm-4">                            <img src="{{frontIdCardPreview}}" class="smImg" alt="..." id="frontIdCardPreview">                            <label class="labelForInput btn btn-info" for="frontIdCard">上传文件</label>                            <input type="file"  id="frontIdCard" class="inputOfFile" ng-src="{{frontIdCard[0].name}}" onchange="angular.element(this).scope().previewImage(‘frontIdCard‘,‘frontIdCardPreview‘)">                        </div>                        <label class="col-sm-2 control-label">身份证反面</label>                        <div class="col-sm-4">                            <img src="{{backIdCardPreview}}" class="smImg" alt="..." id="backIdCardPreview">                            <label class="labelForInput btn btn-info" for="backIdCard">上传文件</label>                            <input type="file" class="inputOfFile" id="backIdCard" ng-src="{{backIdCard[0].name}}" onchange="angular.element(this).scope().previewImage(‘backIdCard‘,‘backIdCardPreview‘)">                        </div>                    </div>                    <div class="form-group">                        <label class="col-sm-2 control-label">驾驶证照片正面</label>                        <div class="col-sm-4">                            <img src="{{photoDriverCardPreview}}" class="smImg" alt="..." id="photoDriverCardPreview">                            <label class="labelForInput btn btn-info" for="photoDriverCard">上传文件</label>                            <input type="file" class="inputOfFile" id="photoDriverCard" ng-src="{{photoDriverCard[0].name}}" onchange="angular.element(this).scope().previewImage(‘photoDriverCard‘,‘photoDriverCardPreview‘)">                        </div>                    </div>                </div>            </div>        </div>    </div>    <div class="text-center">        <button type="button" class="btn btn-info" ng-click="submit()">保存</button>        <button type="button" class="btn btn-default" ui-sref="main.myDriver">返回</button>    </div></form>

  service.js

var uploadFile = function(dataParam,successFn,errFun){
          $http({
              method: ‘POST‘,
              url: configuration.apiBaseUrl+‘/api/tms/upload/uploadFile‘,
              data: dataParam,
              headers: {‘Content-Type‘: undefined},
              transformRequest: angular.identity
          }).success(function (data) {
              if(typeof successFn ===‘function‘){
                  successFn(data);
              }
          }).error(function (data) {
              if(typeof errFun === ‘function‘){
                  errFun(data);
              }
          });
      };

  3:controller.js

 function save(frontIdCard,backIdCard,photoDriverCard) {
            var fd = new FormData();
            if(frontIdCard){
                fd.append("frontIdCard", frontIdCard);
            }
            if(backIdCard){
                fd.append("backIdCard", backIdCard);
            }
            if(photoDriverCard){
                fd.append("photoDriverCard", photoDriverCard);
            }
            myCarService.uploadFile(fd, function (data) {
                var fileArr = data.content;
                for(var i=0; i < fileArr.length; i++){
                    if("frontIdCard" === fileArr[i].fileNameKey){
                        $scope.frontIdCardSrc = fileArr[i].bigImgRtnPath;
                    }else if("backIdCard" === fileArr[i].fileNameKey){
                        $scope.backIdCardSrc = fileArr[i].bigImgRtnPath;
                    }else if("photoDriverCard" === fileArr[i].fileNameKey){
                        $scope.photoDriverCardSrc = fileArr[i].bigImgRtnPath;
                    }
                }
                myDriverService.updateDriver.post({
                    "driverId":$scope.driverId,
                    "name":$scope.name,
                    "idNo":$scope.idNo || "",
                    "mobile":$scope.mobile,
                    "frontIdCard":$scope.frontIdCardSrc,
                    "backIdCard":$scope.backIdCardSrc,
                    "photoDriverCard":$scope.photoDriverCardSrc,
                    "partnerNo": AppSession.getPartnerNo()
                },function(data){
                    if(data.code === 0){
                        messageCenterService.show("操作提示","保存成功", 2000);
                        $state.go(‘main.myDriver‘, {
                        }, {
                            reload: false
                        });
                    }else{
                        messageCenterService.show("操作提示",data.msg, 2000);
                    }
                });
            });
        }

  照片的预览

 $scope.previewImage = function(fileId, imgId) {
            var preview = document.querySelector("#" + imgId);
            var file  = document.querySelector("#" + fileId).files[0];
            var reader = new FileReader();
            reader.onloadend = function () {
                preview.src = reader.result;
            }
            if (file) {
                reader.readAsDataURL(file);
            } else {
                preview.src = "images/noimg.png";
            }
        };

  

时间: 2024-10-06 20:16:44

angular +H5 上传图片 与预览图片的相关文章

2016/4/19 ①单个文件上传 ②上传图片后 预览图片

1,f1.php <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <!-- 作业:在网上找上传图片预览的代码 上传服务器 再预览--> <form action="f1chuli.php&q

H5上传图片前端预览显示

//显示小图片var reader=new FileReader();reader.onload = function ( event ) { var htmlStr = null; var src = null; if (isImage(file)){ src = event.target.result; htmlStr = "<img src='"+src+"'>"; } $("#id").append(htmlStr);};re

h5上传图片并预览

Obj.addEventListener("change",fun) function fun(){ if (this.files && this.files[0]){ var imgSrc=document.querySelector("类"); //获取图片展示节点 var reader = new FileReader(); //判断图片是否加载完毕 reader.onload = function(evt){ if(reader.readyS

asp.net fileupload控件上传图片并预览图片

页面代码: <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="上传" Width="54px&q

一个上传图片,预览图片的小demo

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>3</title> <script> function getFullPath(obj) { if (obj) { //Internet Explorer if (window.navigator.userAgent.indexOf("MSIE") >= 1)

利用formdata异步上传图片并预览图片

<img src="" style="width: 120px;margin-bottom: 5px" id="previewimg0"> <form action="" enctype="multipart/form-data" id="form0"> <input type="file" name="file" i

C#.net4建设网页上传图片前客户端预览图片不能显示

搞了好半天,完全照教学资料设的javascrip脚本,但就是显示不了,后来才发觉,原来客户端要把该网站设为信任才能显示. 所以记录一下,以后备查. <script type="text/javascript">        function showphoto() {            alert(document.getElementById("FileUpload1").value);            //把所选图片的全路径+文件名以“f

模拟type=file;上传并预览图片

最近项目的会员中心,要做一个上传本地img并预览的效果.效果大概是这个样子.自己以前没做过:摸着石头过河总算完成了感觉有必要整理一下,写篇博客耍耍. 整体效果大概是这个样子的: 1.点击框框任何地方:弹出选择文件窗口: 2.选择后,再框框区域预览: 3.再次点击框框,更换图片. 下面说说整个实现的过程吧. html代码的结构大概是这样的.    <div class="imgitem secondImg">        <div class="u-add-

java uploadify上传图片并预览

前一篇文章可以看到对jquery uploadify的属性的讲解,这里给出具体的java代码实现,代码基于servlet,实现了上传图片并预览的效果,不多说,上代码 index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W