最基础的前台上传图片

界面展示:
<el-form-item  prop ="idCardFront" style="height:53px;">
  <p>个人信息页</p>
  <img   @click="fileClick" src="../../assets/images/uploadFile.png">//美化按钮
  <input @change="fileChange($event)" :accept="accept" type="file" id="upload_file" multiple style="display: none"/>//上传图片
</el-form-item>
  <div class="upload_warp_img" v-show="fileArray.length!=0">
    <div  v-for="(item,index) of fileArray" class="upload_warp_img_div">
      <img :src="item.file.src" style="width: 116px;height: 78px">
    </div>
  </div>//图片展示

js方法:

fileClick() {
  document.getElementById('upload_file').click()
},//点击图片调用input中的方法
fileChange:function(el){
  if (!el.target.files[0].size) return;
  this.fileList(el.target);//获取选择的文件
  el.target.value = ''
},
fileList:function (fileList) {//判断上传文件的标准

  let file = fileList.files[0];//真正上传的文件;
  if(file.name.length > 50){
    Msg.error("图片名应在50个字符以内");
    return;
  };
  if(file.size > 5242880){
    Msg.error("图片大小不能超过5M");
    return;
  }
  let reader = new FileReader();
  let _this = this;
  reader.readAsDataURL(file);
  reader.onload = function () {//获取文件的src地址
    file.src = this.result;
      _this.fileArray = [];
    _this.fileArray.push({
      file
    });
    _this.uploadFile(file,value);//调用后台接口
  }
},
uploadFile:function(file,value){
  let _this = this;
    this.loading = true;
   let formData = new FormData();
   formData.append("files", file);
  axios.defaults.timeout = 50000;//防止图片过大,请求超时,传不上去
  axios.post(debug.servers+'/api/file', formData)
    .then(function (response) {
      if(response.data == undefined){
        Msg.error("网络延迟,请重新上传");
        return;
      }

      if(response.data.code == '200'){
        _this.loading = false;
         _this.singleData.idCardFront = response.data.data.fileId;
      }
      else{
        Msg.error('上传失败')
        _this.loading = false;
      }
    }).catch(function (error) {
      _this.loading = false;
      console.log(error);
    });


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值