js上传图片

74 篇文章 5 订阅

js上传图片

vue上传案例如下

<template>
    <div id="upload_img">
        <!--方法一,new FileReader()     base64格式上传 -->
       <div class="uploaded" @click="openBrowser" >
           <input type="file" name="img" imgtype="1"  ref="img" @change="submitImg" />
           <img :src="src"  class="img" v-if="!!this.src" ref="pic"/>
       </div>
       <!--方法二 ,new FormData()   文件上传-->
      <div class="uploaded" @click="openBrowser02" >
        <input type="file" name="img" imgtype="1"  ref="img02" @change="submitImg02" />
        <img :src="src02"  class="img" v-if="!!this.src02" ref="pic02"/>
      </div>
    </div>
</template>

<script>
    export default {
        data(){
          return {
            file:'',
            src:'',
            src02:''
          }
        },
        methods:{
          /*
          *方法一      base64形式,图片上传
          * */
          openBrowser(){
             this.$refs.img.click()
          },
          submitImg(e){
            this.file = e.target.files[0];
            let fr = new FileReader()
            let that = this
            fr.onload =function(e){
               // 获取加载对象的result,即src
               that.src = this.result;
               let data = {
                  img:that.src,
                  imgtype: that.$refs.img.getAttribute('imgtype')
               }
               that.sendImg(data)
            }
            fr.readAsDataURL(this.file)
          },
          sendImg(data){
            this.$ajax.post('/api/v1/save/img',data)
              .then(res=>{
                 console.log(res)
                 this.src = res.data.src;
              })
          },

          /*
          * 方法二    formData 图片上传
          * */
          openBrowser02(){
            this.$refs.img02.click()
          },
          submitImg02(e){
            var formData = new FormData(),
                file = e.target.files[0];
            formData.append('file',file);
            formData.append('type',1);
            this.$ajax({
              method: 'post',
              url: '/api/v1/save/img/src',
              data: formData
            }).then((res)=>{
              this.src02 = res.data.src
              console.log(res)
            })
            
          }
        },
        mounted(){
          this.$nextTick(function () {

          })
        }
    }
</script>

<style scoped>
   .uploaded{
     width: 200px;
     height: 100px;
     border: 1px solid gray;
     margin: 0 auto;
     position: relative;
   }
   .uploaded input{
     display: none;
   }
   .img{
     width: 100%;
     height: 100%;
     position: absolute;
     left: 0;
     top: 0;
   }
   .show{
     display: block;
   }

</style>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值