上传图片的creat一个input来模拟

choosePhoto() {
        const self = this;
        const input = document.createElement('input');
        input.type = 'file';
        input.accept = 'image/x-png,image/gif,image/jpeg';
        input.onchange = function () {
            self.setState({btnState:false});
            self.setState({hasImg:true});
            const reader = new FileReader();
            self.file = this.files.item(0);
            reader.onload = function (e: FileReaderEvent) {
                self.setState({ imgSrc: e.target.result });
            };
            reader.readAsDataURL(self.file);
        }
        input.click();
    }

    upload() {
        if (this.file) {
            this.setState({updateState:!this.state.updateState});
            this.setState({btnState:true});
            upload.upload(this.file, USERICON, 0).then((result) => {
                this.props.callBack(result.name);
                this.setState({updateState:!this.state.updateState});
                this.setState({btnState:false});
                this.context.hide();
            }, (e) => {
                this.setState({updateState:!this.state.updateState});
                this.setState({btnState:false});
                Alert({
                    msg: '上传文件失败,请稍后再试'
                });
            });
        } else {
             if (this.state.imgSrc != '') {
                 this.props.callBack(this.state.imgSrc);
             }
             this.context.hide();
            //Alert({msg:'未选择图片,请选择图片'})
        }
    }

posted on 2017-03-03 10:21  souldawn  阅读(146)  评论(0编辑  收藏  举报

导航