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. 【MariaDB】MariaDB vs MySQL - 特性

    原文链接: https://mariadb.com/kb/en/mariadb-vs-mysql-features/ xiaomo译------ 支持更多的存储引擎 除了标配的MyISAM, BLAC ...

  2. CODEVS1533 Fibonacci数列 (矩阵乘法)

    嗯,,,矩阵乘法最基础的题了. Program CODEVS1250; ..,..] of longint; var T,n,mo:longint; a,b:arr; operator *(a,b:a ...

  3. 浅析gcc、arm-linux-gcc和arm-elf-gcc关系

    浅析gcc.arm-linux-gcc和arm-elf-gcc关系 一.GCC简介 The GNU Compiler Collection,通常简称 GCC,是一套由 GNU 开发的编译器集,为什么是 ...

  4. Mac OSX:最简单的安装ant方法

    安装ant最简单的方法就是通过brew.步骤如下:1. 安装brew(如果已经安装可以跳过这步).ruby -e "$(curl -fsSL https://raw.github.com/m ...

  5. PHP array_intersect_assoc()

    定义和用法 array_intersect_assoc() 函数返回两个或多个数组的交集数组. 与 array_intersect() 函数 不同的是,本函数除了比较键值,还比较键名.返回的数组中元素 ...

  6. 在win10下改变默认的输入法切换Ctrl+Space

    在win10下改变默认的输入法切换Ctrl+Space 学习了:https://www.zhihu.com/question/22288432 在win10下面,有两种风格的控制面板设置: 在输入法上 ...

  7. spring 计时器

    spring 计时器 可以这样: http://blog.csdn.net/u010648555/article/details/52162840 也可以使用annotation <!-- 设置 ...

  8. 阻尼滑动--能够滑动过度的ScrollView(OverScrollView)

    贴上一个我自己用过的阻尼滑动的ScrollView,像QQ里面那种滑动效果,尽管不是我写的,可是我认为还能够,贴出来做个记录,实用到的时候免得到处去找. 代码例如以下: /* * Copyright ...

  9. #定位系统性能瓶颈# sysdig

    安装方法: curl -s https://s3.amazonaws.com/download.draios.com/stable/install-sysdig | sudo bash [root@l ...

  10. MySQL基础教程-绝对推荐

    https://wenku.baidu.com/view/1acfe579ee06eff9aef80752.html?from=search MySQL--公司培训PPT Mysql体系结构以及与Or ...