遇到平台頁面開發需要提交上傳多張證書圖片,之前也做過預覽,但貌似都是單張或者存到隱藏域交給后台處理,那么趁着這次總結了下單張或者多張圖片上傳並且預覽的代碼:
<div class="container"> <label>請選擇一個圖像文件:</label> <input type="file" accept="image/gif,image/png,image/jpeg,image/jpg" id="file_input" multiple/> <button id="submit">提交</button> </div> <ul> <li> <div class="pic_cover"> <img src="http://www.wangzhibo.top/wp-content/uploads/2018/06/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20180426142248-1-300x300.jpg" alt="證書封皮"> <span class="close"></span> </div> </li> </ul>
重點看下input里面的內容 附件上傳屬性 file accept 控制上傳內容的格式 ,這里是圖片 可以 image/* 指所有圖片的格式 也可以指定格式的圖片如: gif,png,jpeg,jpg,
multiple 是 控制否單張或者圖片的關鍵 不寫就是只能單張上傳。
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> window.onload = function(){ var input = document.getElementById("file_input"); var result,fd,dataArr = []; if(typeof FileReader==='undefined'){ alert("抱歉,你的瀏覽器不支持 FileReader"); input.setAttribute('disabled','disabled'); }else{ input.addEventListener('change',readFile,false); } function readFile(){ fd = new FormData(); var iLen = this.files.length; for(var i=0;i<iLen;i++){ if (!input['value'].match(/.jpg|.gif|.png|.jpeg|.bmp/i)){ //判斷上傳文件格式 return alert("上傳的圖片格式不正確,請重新選擇"); } var reader = new FileReader(); fd.append(i,this.files[i]); reader.readAsDataURL(this.files[i]); //轉成base64 reader.fileName = this.files[i].name; reader.onload = function(e){ var imgMsg = { name : this.fileName,//獲取文件名 base64 : this.result //reader.readAsDataURL方法執行完后,base64數據儲存在reader.result里 } dataArr.push(imgMsg); result = '<div class="delete">delete</div><div class="result"><img class="subPic" src="'+this.result+'" alt="'+this.fileName+'"/></div>'; var div = document.createElement('div'); div.innerHTML = result; div['className'] = 'float'; document.getElementsByTagName('body')[0].appendChild(div); //插入dom樹 var img = div.getElementsByTagName('img')[0]; img.onload = function(){ var nowHeight = ReSizePic(this); //設置圖片大小 this.parentNode.style.display = 'block'; var oParent = this.parentNode; if(nowHeight){ oParent.style.paddingTop = (oParent.offsetHeight - nowHeight)/2 + 'px'; } } div.onclick = function(){ $(this).remove(); // 在頁面中刪除該圖片元素 } } } } /*function send(){ var submitArr = []; $('.subPic').each(function () { submitArr.push({ name: $(this).attr('alt'), base64: $(this).attr('src') }); } ); $.ajax({ url : 'http://123.206.89.242:9999', type : 'post', data : JSON.stringify(submitArr), dataType: 'json', //processData: false, 用FormData傳fd時需有這兩項 //contentType: false, success : function(data){ console.log('返回的數據:'+JSON.stringify(data)) } }) }*/ /*oSelect.onclick=function(){ oInput.value = ""; // 先將oInput值清空,否則選擇圖片與上次相同時change事件不會觸發 //清空已選圖片 $('.float').remove(); oInput.click(); } oAdd.onclick=function(){ oInput.value = ""; // 先將oInput值清空,否則選擇圖片與上次相同時change事件不會觸發 oInput.click(); } oSubmit.onclick=function(){ if(!dataArr.length){ return alert('請先選擇文件'); } send(); }*/ } /* 用ajax發送fd參數時要告訴jQuery不要去處理發送的數據, 不要去設置Content-Type請求頭才可以發送成功,否則會報“Illegal invocation”的錯誤, 也就是非法調用,所以要加上“processData: false,contentType: false,” * */ function ReSizePic(ThisPic) { var RePicWidth = 200; //這里修改為您想顯示的寬度值 var TrueWidth = ThisPic.width; //圖片實際寬度 var TrueHeight = ThisPic.height; //圖片實際高度 if(TrueWidth>TrueHeight){ //寬大於高 var reWidth = RePicWidth; ThisPic.width = reWidth; //垂直居中 var nowHeight = TrueHeight * (reWidth/TrueWidth); return nowHeight; //將圖片修改后的高度返回,供垂直居中用 }else{ //寬小於高 var reHeight = RePicWidth; ThisPic.height = reHeight; } }
詳細見代碼介紹,
以及簡單的css樣式
.float{ float:left; width : 200px; height: 200px; overflow: hidden; border: 1px solid #CCCCCC; border-radius: 10px; padding: 5px; margin: 5px; } img{ position: relative; } .result{ width: 200px; height: 200px; text-align: center; box-sizing: border-box; } .delete{ width: 200px; height:200px; position: absolute; text-align: center; line-height: 200px; z-index: 10; font-size: 30px; background-color: rgba(255,255,255,0.8); color: #777; opacity: 0; transition-duration: 0.7s; -webkit-transition-duration: 0.7s; } .delete:hover{ cursor: pointer; opacity: 1; }