最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年06月29日 16:58:25   作者:岳小哥  
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)圖片上傳到后臺(tái),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了vue實(shí)現(xiàn)圖片上傳到后臺(tái)的具體代碼,供大家參考,具體內(nèi)容如下

<!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.js組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

庆云县| 大冶市| 建阳市| 溧阳市| 连州市| 商洛市| 微山县| 喀喇| 林甸县| 金寨县| 湘乡市| 新密市| 湘阴县| 花莲县| 葫芦岛市| 沛县| 泊头市| 靖边县| 焦作市| 饶平县| 老河口市| 呼伦贝尔市| 兴和县| 桃园县| 万宁市| 合肥市| 大厂| 上思县| 镇赉县| 安多县| 宜昌市| 章丘市| 涞源县| 南乐县| 新竹县| 广昌县| 东海县| 陆河县| 海城市| 丰城市| 广宁县|