您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關用vue如何實現圖片上傳到后臺,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="jquery-1.11.3.min.js"></script> <style> .upload { margin: 30px 40px 0; width: 122px; height: 122px; padding-bottom: 40px; position: relative; float: left; } .upload .btn { position: absolute; left: 20px; bottom: 0; width: 80px; height: 30px; line-height: 30px; text-align: center; color: #Fff; border-radius: 5px; background: #ff6c00; } .upload .btn .file { display: inline-block; position: absolute; width: 80px; height: 30px; left: 0; top: 0; opacity: 0; } .upload .btn .add{ position: absolute; left: 0; top: 0; width: 80px; height: 30px; text-align: center; } .upload .imgs { position: absolute; left: 0; top: 0; width: 100px; height: 100px; border: 1px solid #ccc; padding: 10px; } .upload .imgs img { width: 100px; height: 100px; border: 1px solid #f1f1f1; } .upload .imgs .look { position: absolute; left: 10px; top: 10px; width: 100px; height: 100px; line-height: 100px; text-align: center; background: rgba(0, 0, 0, 0.3); color: #fff; } </style> </head> <body> <!-- 模態框 --> <div class="madel-img"></div> <div class="upload_wrap clearfix"> <div class="upload upload1 fl"> <div class="btn"> <span class="add">上傳文件</span> <input type="file" class="file" multiple> <input type="hidden" class="imgUrl"> </div> <div class="imgs"> <img src="shenfenzheng.jpg" alt=""> <div class="look">圖片示范</div> </div> </div> </div> <script> ;(function($){ /* 上傳圖片 */ $.fn.upload = function(id){ this.id = id; this.img = this.id.find($(".imgs img")); this.img_src = this.id.find($(".imgs img")).attr("src"); this.file = this.id.find($(".file")); this.look = this.id.find($(".look")); this.imgUrl = this.id.find($(".imgUrl")); var that = this; this.file.on("change",function(){ var files = this.files;//獲得上傳的所有圖片 var reader = new FileReader();//讀取本地圖片并顯示 var name = files[0].name; var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase(); if(fileName !="jpg" && fileName !="jpeg" && fileName !="png" && fileName !="bmp"){ layer.msg("圖片格式不正確!"); that.img.attr("src",that.img_src) return; } var fileSize = files[0].size; var size = fileSize / 1024; if(size>10000){ layer.msg("圖片不能大于10M"); return; }else if(size <= 0){ layer.msg("圖片不能小于0M"); return; } reader.readAsDataURL(files[0]);//讀取第一張圖片的地址 //數據讀取完成后改變src地址 reader.onload = function(){ that.look.css({"display":"none"}); var image = new Image();//本地緩存一張圖片 var imgCur_src = this.result;//獲取正在上傳的圖片 that.img.attr("src",imgCur_src);//吧剛開始的圖片替換成上傳的圖片 } var fd = new FormData(); fd.append("pic", files[0]); $.ajax({ type: "POST", contentType: false, //必須false才會避開jQuery對 formdata 的默認處理 , XMLHttpRequest會對 formdata 進行正確的處理 processData: false, //必須false才會自動加上正確的Content-Type url: uploadUrl, data: fd, async: false, beforeSend: function (request) { request.setRequestHeader("Authorization", localStorage.token); request.setRequestHeader("X-Requested-With", "XMLHttpRequest"); }, success: function (msg) { console.log(msg) if (msg.code == "100") { imgUrl.val(msg.data); } }, error: function (msg) { console.log(msg); } }); }) } $(".upload1").upload($(".upload1")); })(jQuery) </script> </body> </html>
關于用vue如何實現圖片上傳到后臺就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。