zoukankan      html  css  js  c++  java
  • H5调用相机或照片

    <input type="file" accept="image/*;capture=camera">

    不加capture会打开两个

     uploadImgClick = () => {
            const that = this
            const uploadInput = document.createElement('input')
            uploadInput.style.display = 'none'
            uploadInput.type = 'file'
            uploadInput.accept = 'image/*'
            // uploadInput.capture = "camera"
            uploadInput.addEventListener('change', evt => {
                let file = evt.target.files[0]
                var reader = new FileReader()
    
                // 绑定load事件
                // const { fileName, base64Str } = imageItem;
                reader.readAsDataURL(
                    document.querySelector('input[type=file]').files[0]
                )
                reader.onload = function (e) {
                    that.setState({
                        loadingStatus: true
                    }, () => {
                        fetch('/ui-api/aliyun/uploadBase64ImageSimple', {
                            credentials: 'include',
                            method: 'POST',
                            headers: {
                                'Content-type': 'application/json'
                            },
                            body: JSON.stringify({
                                fileName: file.name,
                                base64Str: e.target.result
                            })
                        })
                            .then(res => {
                                that.setState({
                                    loadingStatus: false
                                })
                                return res.json()
                            })
                            .then(res => {
                                // 用完需要清除掉,要不会一直取第一个
                                document.body.removeChild(uploadInput)
                                if (res.code === 0) {
                                    that.setState({
                                        imgUrl: res.data.url
                                    })
                                }
                            })
                    })
    
                }
    
            })
    
            document.body.appendChild(uploadInput)
    
            const evt = document.createEvent('MouseEvents')
            evt.initMouseEvent('click', false, false)
            uploadInput.dispatchEvent(evt)
        }
    

      

  • 相关阅读:
    Linux 修改 root密码
    python实现 CI/CD(jenkins+gitlab)
    redis集群
    土木工程材料0732
    C语言程序设计【1032】
    汽车文化【1196】
    应用写作0045
    思想道德修养与法律基础[1053]
    英语【0002】
    社区管理【0272】
  • 原文地址:https://www.cnblogs.com/dongkx/p/11189835.html
Copyright © 2011-2022 走看看