分为两个文件夹,index.js(逻辑文件)    styled.js(样式文件)

index.js文件,编写完成之后在对应的地方引入即可


import React from "react"
import { UploadContainer } from "./styled"
import { Icon ,message} from "antd"
import { fetch as fetchPro } from "whatwg-fetch"
class Upload extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            imgUrl: ""
        }
        if (this.props.value) {
            this.state.imgUrl = this.props.value
        }
    }
    render() {
        let { imgUrl } = this.state
        return (
            <UploadContainer>
                <input type="file" onChange={this.handleUpdate.bind(this)} ref="files" />
                <div className="imgContent">
                    {imgUrl ? <img src={imgUrl} /> : <Icon type="plus" style={{ fontSize: 26 }} />}
                </div>
            </UploadContainer>
        )
    }
    async handleUpdate() {
        let fileImg = this.refs.files.files[0];         let formData = new FormData()         formData.append("filesImg", fileImg)//第一个参数为后端规定字段,第二个参数时需要上传的图片         let data = await fetchPro("/ajax/upload/files", {
            method: "post",
            body: formData
        }).then(res => res.json())//第一个参数为地址,第二个参数为配置项
        
        if(data.data.urlPath){
            this.setState({
                imgUrl:data.data.urlPath
            })
        }else{
            message.error(data.data.info)
        }
    }
} export default Upload

styled.js文件

import styled from "styled-components"

export const UploadContainer = styled.div`
width:110px;
height:110px;
border:1px dashed #ccc;
position:relative;
input{
opacity:0;
width:110px;
height:100%;
position:absolute;
left:0;
top:0;
z-index:2;
}
.imgContent{
padding:5px;
width:100%;
height:100%;
position:absolute;
left:0;
top:0;
display:flex;
justify-content:center;
align-items:center;
img{
width:100%;
height:100%;
}
}
`

最新文章

  1. java判断request请求是手机还是pc终端
  2. python 基于windows环境的ftp功能
  3. 15分钟学会使用Git和远程代码库
  4. postgresql 获取刚刚插入的数据主键id
  5. Caffe 深度学习框架介绍
  6. eclipse中tomcat加gc日志输出
  7. python实现 _ 图书馆书籍到期之前_自动邮件提醒
  8. Options for Debugging Your Program or GCC
  9. MongoDB - Introduction to MongoDB, MongoDB Extended JSON
  10. Android学习系列(28)--App集成支付宝[已过期]
  11. Android 入门第一课 一个简单的提示框
  12. jquery的插件机制
  13. jetty运行maven程序(修改及时生效,不需要重启jetty程序)
  14. python alembic which comes from SQLalchemy
  15. C#内部关于绑定事件Event的线程安全
  16. Ubuntu 16.04 安装Google 浏览器
  17. Ubuntu下搭建spark2.4环境(单机版)
  18. 10-ajax技术简介
  19. 765. 有效的三角形.md
  20. .Net Core资源

热门文章

  1. JS相比TS的缺点(或TS的优点)
  2. Kali系统 metasploit 使用教程
  3. Neo4j : 通过节点的 id属性 对节点进行查,改,删操作
  4. JAVA一个文件写多个类
  5. 记录面试一位三年经验Web前端开发者的过程
  6. centos7卸载YUM后重装过程 -bash: yum: command not found / -bash: yum: 未找到命令
  7. windows 2003 系统管理员debug
  8. shell 数组中 @ 跟 * 的区别
  9. uboot中Kconfig架构的理解
  10. 转发一个robotframework的循环