RushMan
RushMan
  • 发布:2022-03-01 11:31
  • 更新:2024-04-15 17:04
  • 阅读:670

考虑用renderjs做app和h5端的上传

分类:uni-app
<template>  
</template>  
<script>  
    export default {  
        methods: {  
            chooseAndUpload() {  
                this.chooseFile(() => {  

                })  
            },  
            chooseFile(cb) {  
                // #ifdef APP-VUE || H5  
                this.file.choose((files) => {  
                    cb && cb(files)  
                })  
                // #endif  
                // #ifdef MP-WEIXIN  
                this.chooseFromWX((res) => {  
                    console.log(res);  
                    cb && cb(res)  
                })  
                // #endif  
            },  
            chooseFromWX(cb) {  
                wx.chooseMessageFile({  
                    count: 9,  
                    type: 'file',  
                    success(res) {  
                        cb && cb(res);  
                    }  
                })  
            }  
        },  
    }  
</script>  
<script module="file" lang="renderjs">  
    let inputEl = {  
        el: null,  
        inject(cb) {  
            if (!this.el) {  
                let input = document.createElement("input")  
                input.type = "file"  
                input.id = "_file"  
                input.style.display = "none"  
                input.addEventListener("change", cb)  
                this.el = input  
                document.body.append(input)  
            }  
            this.el.click()  
            return this.el  
        },  
        remove(cb) {  
            if (this.el) {  
                this.el.removeEventListener("change", cb)  
                this.el.remove()  
                this.el = null  
            }  
        }  
    }  
    export default {  
        methods: {  
            _success() {},  
            choose(cb) {  
                inputEl.inject(this.fileChange)  
                cb && (this._success = cb)  
            },  
            fileChange(e) {  
                this._success && this._success(e.target.files)  
                inputEl.remove(this.fileChange)  
            }  
        }  
    }  
</script>  
<style>  
</style>

提供一种思路,进测试能够再我的安卓机上选择文件,别的还需要完善,各位有指教的话请发表一下自己的思路

0 关注 分享

要回复文章请先登录注册

2***@qq.com

2***@qq.com

回复 aak12345 :
renderjs 是无法直接获的uni实例的
2024-04-15 17:04
aak12345

aak12345

https://uniapp.dcloud.net.cn/api/window/communication.html uni主动发消息给iframe,可以在renderjs里面添加uni.$on,然后在uni里面uni.$emit
2023-11-08 19:22