vue实现图片上传到后台

所属分类: 网络编程 / JavaScript 阅读数: 1505
收藏 0 赞 0 分享

本文实例为大家分享了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.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

layui table 复选框跳页后再回来保持原来选中的状态示例

今天小编就为大家分享一篇layui table 复选框跳页后再回来保持原来选中的状态示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Vue-Cli项目优化操作的实现

这篇文章主要介绍了Vue-Cli项目优化操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解决包含在label标签下的checkbox在ie8及以下版本点击事件无效果兼容的问题

这篇文章主要介绍了解决包含在label标签下的checkbox在ie8及以下版本点击事件无效果兼容的问题,本文给大家总结的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

vue 父组件通过v-model接收子组件的值的代码

这篇文章主要介绍了vue 父组件通过v-model接收子组件的值的代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

vue 全局环境切换问题

小编在开发使经常会碰到全局切换问题,今天小编给大家带来一篇教程给大家介绍vue 全局环境切换问题,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

element-ui 本地化使用教程详解

这篇文章主要介绍了element-ui 本地化使用教程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

在Vue项目中,防止页面被缩放和放大示例

今天小编就为大家分享一篇在Vue项目中,防止页面被缩放和放大示例,具有很好的参考 价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

vue h5移动端禁止缩放代码

今天小编就为大家分享一篇vue h5移动端禁止缩放代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Vue 3.0双向绑定原理的实现方法

这篇文章主要为大家详细介绍了Vue 3.0双向绑定原理的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

nest.js 使用express需要提供多个静态目录的操作方法

这篇文章主要介绍了nest.js 使用express需要提供多个静态目录的操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多