import React from 'react'
import {Upload, Icon, message,Button } from 'antd'
import './index.scss';
import Axios from 'axios'
import { resolve } from 'path';
// import { resolve } from 'dns';
function getBase64(img, callback) {
const reader = new FileReader();
reader.addEventListener('load', () => callback(reader.result));
reader.readAsDataURL(img);
}
 
export default class Uploading extends React.Component{
constructor(props){
super(props)
}
state = {
loading: false,
imageUrl:''
};
handleChange = (info) => {
console.log('info-->', info)
// if (info.file.status === 'uploading') {
// this.setState({ loading: true });
// return;
// }
// if (info.file.status === 'done') {
// // Get this url from response in real world.
// getBase64(info.file.originFileObj, imageUrl => this.setState({
// imageUrl,
// loading: false,
// }));
// }
const isJPG = info.file.type === 'image/jpeg';
const isPNG = info.file.type === 'image/png';
if (!isJPG && !isPNG) {
message.error('仅支持JPG,JPEG,PNG');
}
const isLt1M = info.file.size / 1024 /1024 < 1;
if (!isLt1M) {
message.error('图片限制1M以下');
}
if (!((isJPG || isPNG) && isLt1M)) {
return false;
}
let formData = new window.FormData()
formData.append('file', info.file, info.file.name)
Axios({
headers: {
'Content-Type': 'multipart/form-data'
},
method: 'post',
data: formData,
url: 'http://192.168.5.14:8081/node/file_upload'
}).then(res => {
console.log('res--->', res)
console.log(11,this)
if (res.data.code === 200) {
let imgurl=res.data.result[0].photoBig
this.setState({
imageUrl:'http://192.168.5.14:8081/'+imgurl
})
}
}, err => {
console.log('err', err)
})
}
beforeUpload(file) {------------------------1.要么beforeUpload直接返回false,所有的验证前的操作和上传文件都在onChange事件里进行,这样upload插件有没有action都无所谓
------------------2.要么就是beforeUpload返回一个promise对象,进行异步校验,但是此时如果要自己手动上传就不用传action,否则就会进行2次上传,但是如果自己不写异步请求,也可以用它的action进行上传,但是没法拿到数据
return false
// console.log(file)
// return new Promise((resolve, reject) =>{
// console.log(222222)
// const isJPG = file.type === 'image/jpeg';
// const isPNG = file.type === 'image/png';
// if (!isJPG && !isPNG) {
// message.error('仅支持JPG,JPEG,PNG');
// }
// const isLt1M = file.size / 1024 /1024 < 1;
// if (!isLt1M) {
// message.error('图片限制1M以下');
// }
// if (!((isJPG || isPNG) && isLt1M)) {
// return false;
// }
// let formData = new window.FormData()
// formData.append('file', file, file.name)
// Axios({
// headers: {
// 'Content-Type': 'multipart/form-data'
// },
// method: 'post',
// data: formData,
// url: 'http://192.168.5.14:8081/node/file_upload'
// }).then(res => {
// console.log('res--->', res)
// console.log(11,this)
// if (res.data.code === 200) {
// let imgurl=res.data.result[0].photoBig
// this.setState({
// imageUrl:'http://192.168.5.14:8081/'+imgurl
// })
// resolve()
// }
// }, err => {
// console.log('err', err)
// })
// })
}
render(){
const uploadButton = (
<div className='uploadTit'>
<div>标题图片</div>
<div>426 x 240</div>
</div>
);
 
const imageUrl = this.state.imageUrl;
return(
<div className='upload'>
<div className='uploadJpg'>
{imageUrl ? <img src={imageUrl} alt="avatar" /> : uploadButton}
</div>
<Upload
name="file"//发到后台的文件参数名
className="avatar-uploader"
showUploadList={false}//是否展示uploadList
// action="http://192.168.5.14:8081/node/file_upload"//必选参数,上传的地址
// beforeUpload={this.beforeUpload.bind(this)}//上传文件的钩子
beforeUpload={this.beforeUpload}
onChange={this.handleChange}//上传文件改变时的状态
>
<Button type='primary' className='replacebtn222'>
上传文件图片
</Button><br/>
<p style={{marginTop:10}}>大小426 * 240像素,图片限制1M以下,仅支持JPG,JPEG,PNG</p>
</Upload>
 
 
 
</div>
)
 
}
}

antd #upload的更多相关文章

  1. 我们一起踩过的坑----react(antd)(二)

    1.antd Upload默认值问题 需求是这样的,后台若没有图片默认值,则只有上传按钮,且只能上传一张图片:若有默认值,则显示默认头像图片, 可删除,删除之后有且只能添加一张图片,没有删除默认图片时 ...

  2. antd pro table中的文件上传

    概述 示例代码 列表页面 form 页面 model.js service.js 总结 概述 项目中经常会遇到在表格中展示图片的需求(比如展示用户信息时, 有一列是用户的头像). antd pro t ...

  3. React + js-xlsx构建Excel文件上传预览功能

    首先要准备react开发环境以及js-xlsx插件 1. 此处省略安装react安装步骤 2.下载js-xlsx插件 yarn add xlsx 或者 npm install xlsx 在项目中引入 ...

  4. Web 开发中 Blob 与 FileAPI 使用简述

    本文节选自 Awesome CheatSheet/DOM CheatSheet,主要是对 DOM 操作中常见的 Blob.File API 相关概念进行简要描述. Web 开发中 Blob 与 Fil ...

  5. 前端AntD框架的upload组件上传图片时遇到的一些坑

    前言 本次做后台管理系统,采用的是 AntD 框架.涉及到图片的上传,用的是AntD的 upload 组件. 前端做文件上传这个功能,是很有技术难度的.既然框架给我们提供好了,那就直接用呗.结果用的时 ...

  6. React antd如何实现<Upload>组件上传附件再次上传已清除附件缓存问题。

    最近在公司做React+antd的项目,遇到一个上传组件的问题,即上传附件成功后,文件展示处仍然还有之前上传附件的缓存信息,需要解决的问题是,要把上一次上传的附件缓存在上传成功或者取消后,可以进行清除 ...

  7. 关于本地使用antd的upload组件上传文件,ngnix报错405的问题

    使用阿里的ui框架antd的upload,会自动请求ngnix上面的一个路径,也就是action所在的位置,一直报错405 not allowed,后来经讨论,统一将action写成一个路径,后端对这 ...

  8. AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为

    本次做后台管理系统,采用的是 AntD 框架.涉及到图片的上传,用的是AntD的 upload 组件. 我在上一篇文章<AntD框架的upload组件上传图片时使用customRequest方法 ...

  9. 封装react antd的upload上传组件

    上传文件也是我们在实际开发中常遇到的功能,比如上传产品图片以供更好地宣传我们的产品,上传excel文档以便于更好地展示更多的产品信息,上传zip文件以便于更好地收集一些资料信息等等.至于为何要把上传组 ...

随机推荐

  1. static 语句块

    static{}(即static块),会在类被加载的时候执行且仅会被执行一次,一般用来初始化静态变量和调用静态方法,下面我们详细的讨论一下该语句块的特性及应用. 一.在程序的一次执行过程中,stati ...

  2. hdu_1020_Encoding_201310172120

    Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  3. Linux C程序存储空间的逻辑布局

    原文:http://blog.chinaunix.net/uid-20692625-id-3057053.html ------------------------------------------ ...

  4. 使用汇编分析c代码的内存分布

    arm平台下使用反汇编分析c内存分布: arm:使用arm-linux-objdump命令将编译完毕之后的elf文件,进行反汇编. 之后重定向到tmp.s文件里. 第一步变量例如以下c文件. vim ...

  5. Linux局域网搭建

    系统版本号:Linux red hat 6.3 1. 打开ifcfg-eth0 # cd /etc./sysconfig/network-scripts # vim ifcfg-eth0 2. 改动i ...

  6. DOCKER_HOST have a weird tcp

    [piqiu@benjaminpro ~]$boot2docker start Waiting for VM and Docker daemon to start... ............... ...

  7. 常见的DP优化类型

    常见的DP优化类型 1单调队列直接优化 如果a[i]单调增的话,显然可以用减单调队列直接存f[j]进行优化. 2斜率不等式 即实现转移方程中的i,j分离.b单调减,a单调增(可选). 令: 在队首,如 ...

  8. Privoxy shadowscocks代理

    ubuntu已经启动好了sock5的代理, 代理为: 127.0.0.1:1080. #使用Privoxy将sock5代理映射为http代理. 安装Privoxy sudo apt-get updat ...

  9. hdu1704——floyd

    Problem Description there are N ACMers in HDU team.ZJPCPC Sunny Cup 2007 is coming, and lcy want to ...

  10. 【POJ 3635】 Full Tank

    [题目链接] http://poj.org/problem?id=3635 [算法] 优先队列BFS 实现类似于堆优化dijkstra [代码] #include <algorithm> ...