请问element-ui 上传图片如何修改成base64格式的啊?

<el-upload

  class="upload-demo"

  action="https://jsonplaceholder.typicode.com/posts/"

  :on-preview="handlePreview"

  :on-remove="handleRemove"

  :file-list="fileList">

  <el-button size="small" type="primary">点击上传</el-button>

  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>

</el-upload>

<script>

  export default {

    data() {

      return {

        fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]

      };

    },

    methods: {

      handleRemove(file, fileList) {

        console.log(file, fileList);

      },

      handlePreview(file) {

        console.log(file);

      }

    }

  }

</script>

上面是element-ui的官方组件


翻阅古今
浏览 2085回答 1
1回答

慕侠2389804

看了一下源码,file 里有个属性 raw , 其实就是rawFile把钩子挂在preview 上为例:previewBanner(file){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let reader = new FileReader()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.onload = () => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let _base64 = reader.result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doWhatUWant(_base64)//就可以做你想做的啦!!!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.readAsDataURL(file.raw)&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript