简体   繁体   English

将图像转换为base64时如何显示进度条?

[英]How to display progress bar while converting image to base64?

I upload an image, then the program show it: converting the uploaded image to base64 and put it in img src. 我上传一个图像,然后程序显示它:将上传的图像转换为base64并将其放入img src中。 What i'm trying to achieve is while waiting to large image convert to base64 display a progress bar. 我想要实现的是在等待大图像转换为base64时显示进度条。

I tried to show progress bar before converting, and then hide progress bar when finished. 我尝试在转换之前显示进度条,然后在完成后隐藏进度条。

<div>
   <v-avatar v-if="editedItem.image" class="mt-3" :tile="true" :size="250" color="grey lighten-4"><img :src="editedItem.image" alt="avatar"></v-avatar>
   <input id="image" type="file" accept="image/*" multiple="false" ref="imageUpload" v-show="false" @change="setImage()" />
   <v-text-field class="centered-input" v-model="editedItem.imageStatus" name="image" :rules="imageRules" readonly v-show="!seeingItem"></v-text-field>
   <v-btn outline block color="black" @click.native="imageUpload()" v-show="!seeingItem">{{ imageButtonText }}</v-btn>
</div>
    imageUpload () {
      this.$refs.imageUpload.click() // Launch file dialog
    },

    setImage () {
      this.loading = true // Init progress bar

      let image = document.getElementById('image') // Get image

      if (image.files && image.files[0]) { // Check if image and start converting
        var fileReader = new FileReader()

        fileReader.addEventListener('load', e => {
          this.editedItem.image = e.target.result
        })

        fileReader.readAsDataURL(image.files[0])

        this.editedItem.imageStatus = 'Imagen Cargada'
        this.imageButtonText = 'Cambiar Imagen'
      } // Finished converting

      this.loading = false // Hide progress bar
    },

The problem is that the progress bar is not showing, I don't why certainly. 问题是进度条没有显示,我不知道为什么。 I think it's because I set to true loading var to show the progress bar but the program starts rapidly to convert the image and doesn't wait to the progress bar to actually show because the program got ocuppied in converting the image. 我认为这是因为我设置为true loading var以显示进度条,但是该程序开始快速转换图像,而没有等到进度条实际显示,因为该程序在转换图像时被占用。

As Brad mentioned in the comment, is a good workaround to avoid converting to base64 since blob is faster. 正如布拉德(Brad)在评论中提到的那样,这是避免转换为base64的好方法,因为blob更快。 Thanks, Brad. 谢谢,布拉德。 Here's the code without converting to base64: 这是不转换为base64的代码:

<div>
   <v-avatar v-if="editedItem.image" class="mt-3" :tile="true" :size="250" color="grey lighten-4"><img :src="editedItem.image" alt="avatar"></v-avatar>
   <input id="image" type="file" accept="image/*" multiple="false" ref="imageUpload" v-show="false" @change="setImage($event)" />
   <v-text-field class="centered-input" v-model="editedItem.imageStatus" name="image" :rules="imageRules" readonly v-show="!seeingItem"></v-text-field>
   <v-btn outline block color="black" @click.native="imageUpload()" v-show="!seeingItem">{{ imageButtonText }}</v-btn>
</div>
    imageUpload () {
      this.$refs.imageUpload.click() // Launch file dialog
    },

    setImage (event) {
      let image = document.getElementById('image') // Get image

      if (image.files && image.files[0]) { // Check if image
        this.editedItem.image = URL.createObjectURL(event.target.files[0]) // Get blob

        this.editedItem.imageStatus = 'Imagen Cargada'
        this.imageButtonText = 'Cambiar Imagen'
      } // Finished converting
    }

This workaround works, but still want to know a solution to the exposed problem. 此解决方法有效,但仍想知道所暴露问题的解决方案。

Your guess is correct. 你的猜测是正确的。

Try starting the conversion a bit later: 稍后尝试启动转换:

setImage() {
    this.loading = true // Init progress bar
    const _this = this;
    setTimeout(() => {

        let image = document.getElementById('image') // Get image

        if (image.files && image.files[0]) { // Check if image and start converting
            var fileReader = new FileReader()

            fileReader.addEventListener('load', e => {
                this.editedItem.image = e.target.result
            })

            fileReader.readAsDataURL(image.files[0])

            _this.editedItem.imageStatus = 'Imagen Cargada'
            _this.imageButtonText = 'Cambiar Imagen'
        } // Finished converting

        _this.loading = false // Hide progress bar
    }, 100);
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM