分为两个文件夹,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%;
}
}
`

React后台管理手动封装图片上传组件的更多相关文章

  1. 微信小程序简单封装图片上传组件

    微信小程序简单封装图片上传组件 希望自己 "day day up" -----小陶 我从哪里来 在写小程序的时候需要上传图片,个人觉得官方提供的 Uploader 组件不是太好用, ...

  2. 基于Node的React图片上传组件实现

    写在前面 红旗不倒,誓把JavaScript进行到底!今天介绍我的开源项目 Royal 里的图片上传组件的前后端实现原理(React + Node),花了一些时间,希望对你有所帮助. 前端实现 遵循R ...

  3. 分享一个react 图片上传组件 支持OSS 七牛云

    react-uplod-img 是一个基于 React antd组件的图片上传组件 支持oss qiniu等服务端自定义获取签名,批量上传, 预览, 删除, 排序等功能 需要 react 版本大于 v ...

  4. vue图片上传组件

    前言:很多项目中都需要用到图片上传功能,而其多处使用的要求,为了避免重复造轮子,让我决定花费一些时间去深入了解,最终封装了一个vue的图片上传组件.现将总结再次,希望有帮助. Layout <d ...

  5. H5拍照、选择图片上传组件核心

    背景 前段时间项目重构,改成SSR的项目,但之前用的图片选择上传组件不支持SSR(server-side-render).遂进行了调研,发现很多的工具.但有的太大,有的使用麻烦,有的不满足使用需求.决 ...

  6. Jquery图片上传组件,支持多文件上传

    Jquery图片上传组件,支持多文件上传http://www.jq22.com/jquery-info230jQuery File Upload 是一个Jquery图片上传组件,支持多文件上传.取消. ...

  7. 前端nginx+Java后台ftp处理页面图片上传踩坑

    今天,将前端代码部署到服务器nginx上,在测试多图片上传时,报错413请求体空间太大,请求都没到后台,直接被nginx拦截,调整后又报错504. 整体而言,前端存在两处问题: 413 错误 :Req ...

  8. 图片上传组件webuploader

    前端组件webuploader 当时也是搞了很久参考这种demo,但是没记.现在事后大致总结下.直接上大概代码(我使用asp.net  MVC来做的): 执行顺序:(get)Record/Add——A ...

  9. vux-uploader 图片上传组件

    1.网址:https://github.com/greedying/vux-uploader 2.安装 npm install vux-uploader --save npm install --sa ...

随机推荐

  1. 问题 C: 序列交换

    问题 C: 序列交换 时间限制: 1 Sec  内存限制: 128 MB提交: 914  解决: 48[提交] [状态] [命题人:jsu_admin] 题目描述 给一个 1 到 n 的排列,每次可以 ...

  2. StringBuffer 和 StringBuilder 总结

    StringBuffer 和 StringBuilder 介绍 大多数情况下, StringBuffer 的速度要比 String 快: StringBuilder 要比StringBuffer快:S ...

  3. web框架的本质(使用socket实现的最基础的web框架、使用wsgiref实现的web框架)

    import socket def handle_request(client): data = client.recv(1024) client.send("HTTP/1.1 200 OK ...

  4. NGUI的HUD Text的扩展插件学习--(HUDText)的使用

    一,我们先添加一个空的游戏对象,在菜单中找到这个添加空的游戏对象 二,然后我们给该对象添加HUDText,然后给这个添加字体 三,我们添加个脚本,代码如下: using UnityEngine; us ...

  5. Antd-react-mobile项目学习中遇到的问题记录(持续更新)

    1.Error:The "injectBabelPlugin" helper has been deprecated as of v2.0. You can use customi ...

  6. Echarts数据可视化grid直角坐标系(xAxis、yAxis)详解:

    mytextStyle={ color:"#333", //文字颜色 fontStyle:"normal", //italic斜体 oblique倾斜 font ...

  7. Redis 复制功能详解

    Redis 复制功能的几个重要方面: 1. 一个Master可以有多个Slave:2. Redis使用异步复制.从2.8版本开始,Slave会周期性(每秒一次)发起一个Ack确认复制流(replica ...

  8. 阿里云轻应用云服务器配置tomcat

    #etc/profile export CATALINA_HOME=/wocloud/tomcat_cluster/tomcat1/apache-tomcat-7.0.57 #查看防火墙状态 fire ...

  9. [web 安全] 源码泄露

    web 源码泄露 1..hg 源码泄露 http://www.example.com/.hg/ 2..git 源码泄露 http://www.example.com/.git/config 3..ds ...

  10. phpstorm 调试时浏览器显示The requested resource / was not found on this server

    1.进入thinkphp项目的public 目录运行以下命令即可 root@jiang:/var/www/tp5# php -S localhost:8080 router.php PHP 7.2.2 ...