从文件中选择还可以,无法从相机上传照片

时间:2018-11-13 13:07:59

标签: mobile input camera upload photo

我打算从手机上载照片,并且当我从存储中选择文件时它可以工作。拍照并上传到我的服务器后,它直接遇到Axios的错误功能。

uploadImage (e) {
            var sequence = e.target.dataset.number
            const that = this         
            var elem = e.target
            var file = e.target.files[0]
            var imgUrl = window.URL.createObjectURL(file)
            this.galleryArr[sequence] = imgUrl
            var img = new Image()
            img.src = imgUrl
            img.setAttribute('number', sequence)
            var li = document.createElement('li')
            li.appendChild(img)
            elem.parentNode.previousElementSibling.innerHTML = ''
            elem.parentNode.previousElementSibling.appendChild(li)
            const url = 'xxxxxxxxxxxx'
            var reader = new FileReader(),
            formData = new FormData();
            reader.onload = function (e) {
                e = e || window.event;
                var dataurl = e.target.result,
                    blob = that.dataURLtoBlob(dataurl);
                formData.set('content', blob, file.name);
                formData.set('access_token', that.sign)
                that.$axios({
                    method: 'post',
                    url,
                    data: formData
                }).then(res => {
                    if (res.data.errcode === 0) {
                        that.weui.alert(res.data.data.url,{ title: '上传成功' 
                 })
                        that.urlArr[sequence] = res.data.data.url
                    } else {
                        that.weui.alert(res.data.errcode)
                    }
                }).catch(err => {
                    that.weui.alert('暂不支持拍照上传,请从相册中选照片')
                    console.log(err)
                });
            };

0 个答案:

没有答案