uniapp blob转base64,解决new FileReader在APP中无法使用的问题

1.解决办法通过renderjs,来操作bom

1.假设您已经拿到blob:url,废话不多说,直接上代码如下

<template>
     <view :change:prop="lzupload.onChange" :prop="propFile"></view>
</template>

<script>
    let that
    import {
        mapGetters,
    } from 'vuex'
    export default {
        name: 'UniPopupShare',
        data() {
            return {//这是你获取到的文件对象,假设里面的path就你的blob:url
                propFile:{
           path:""   
         }
, } }, mounted() { that = this }, methods: { //假设这个函数是,你选取文件获取到的文件对象,此对象里面有blob:url,
       //这时我们把它传递到render.js里,通过render.js来转化base64,当然你也可以做其他bom或dom操作
onChange(files) {
          //我这里写死个,实际要用你获取到的blob
          
this.propFile={
            path:'blob:file:///de58c9d4-b679-4acc-80b2-d1fb9eaadeee'
         }
      }
      changeBase64(data){}
        }
    }
</script>
<script module="lzupload" lang="renderjs">
    export default {
        data() {
            return {
            }
        },
        mounted() {},
        methods: {//选取的文件上传
            onChange(newValue, oldValue, ownerInstance, instance){
                console.log(newValue);
                let that = this;
                var xhr = new XMLHttpRequest()
                xhr.open('GET', newValue.path, true)
                xhr.responseType = 'blob'
                xhr.onload = function() {
                    if (this.status === 200) {
                        let fileReader = new FileReader()
                        fileReader.onload = function(e) {
                            console.log('blob转bas64成功:',e.target.result.slice(0,88))
                 
newValue.base64=e.target.result
                //把获取到的base64传递给service 层的方法
                ownerInstance.callMethod('changeBase64',newValue)
              }
                        fileReader.onerror = function(err){
                            console.log(err);
                        }
                        fileReader.readAsDataURL(this.response)
                    }
                }
                xhr.onerror = function(e){
                    console.log(e);
                }
                xhr.send()
            }
        }
    }
</script>
<style lang="scss">
    
</style>

 

newValue
posted @ 2022-08-05 10:48  奔跑吧前端(李钊)  阅读(5723)  评论(0编辑  收藏  举报