溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

用vue如何實(shí)現(xiàn)圖片上傳到后臺(tái)

發(fā)布時(shí)間:2020-06-30 09:14:15 來(lái)源:億速云 閱讀:1052 作者:清晨 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)用vue如何實(shí)現(xiàn)圖片上傳到后臺(tái),小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

<!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>
 <!-- 模態(tài)框 -->
 <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]);//讀取第一張圖片的地址
   //數(shù)據(jù)讀取完成后改變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才會(huì)避開jQuery對(duì) formdata 的默認(rèn)處理 , XMLHttpRequest會(huì)對(duì) formdata 進(jìn)行正確的處理
     processData: false, //必須false才會(huì)自動(dòng)加上正確的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>

關(guān)于用vue如何實(shí)現(xiàn)圖片上傳到后臺(tái)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI