<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)
    }