input实现手机拍照

复制代码
<div id="demo">
    <img :src="imgurl" width="200px" height="200px" alt="">
    <input style="display: none;" type="file" accept="image/*" id="cameraInput" capture="camera" @change="selectPhoto(event)">
    <div @click="clickBrn">点击上传</div>
</div>

<script>
    let app=new Vue({
        el: "#demo",
        data() {
            return {
                imgurl:""
            }
        },

        methods: {
            clickBrn(){
                document.getElementById("cameraInput").click();
            },
            selectPhoto(e){
                var that=this
                var file = e.target.files[0];
                // 创建FileReader对象
                var reader = new FileReader();
                // 读取文件内容
                reader.onload = function (e) {
                    var imageDataURL = e.target.result;
                    // console.log(imageDataURL,"img")
                    that.imgurl=imageDataURL
                    that.uploadImage(imageDataURL);
                };
                reader.readAsDataURL(file);
            },
            // 上传图像
            uploadImage(imageDataURL) {
                fetch('./your url', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: imageDataURL
                }).then(function (response) {
                    return response.text();
                }).then(data=>{
                    console.log(data)
                }).catch(function (error) {
                    console.log(error);
                });
            },
        },
})    
复制代码

 

posted @   eternityQSL  阅读(66)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示