js上传图片获取原始宽高

我怕爱的太早我们不能终老 提交于 2019-12-05 23:30:35

以vue上传图片为例:

<template>
  <div>
    <input type="file" @change="uploadFile($event)" multiple="multiple" />
  </div>
</template>
 
<script>
  export default {
    name: 'upload',
    data () {
      return {
        imgInfo: {}
      }
    },
    methods:{
      uploadFile(event){
        let that = this;
        let file = event.target.files[0];
        let fileReader = new FileReader();
        fileReader.readAsDataURL(file); //根据图片路径读取图片
        fileReader.onload = function(e) {
          let base64 = this.result;
          let img = new Image();
          img.src = base64;
          img.onload = function() {
            that.imgInfo = {
              width: img.naturalWidth,
              height: img.naturalHeight
            };
            console.log("宽:" + img.naturalWidth + " 高:" + img.naturalHeight);
          }
        }
      }
    }
  }
</script>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!