react antv实现图片上传预览

Bertha 。 2021-07-24 23:32 874阅读 0赞
  1. 1、引入
  2. import {Upload} from 'antd'
  3. 全局引入样式:import 'antd/dist/antd.css'
  4. 2、使用
  5. <Upload
  6. name="上传名称,和后台约定"
  7. listType="picture-card"
  8. className="样式类"
  9. showUploadList={false}
  10. action="后台地址"
  11. onChange={上传图片回调,第一个参数为图片信息}
  12. >
  13. 图片预览
  14. {imageUrl ? <img src={"http://localhost:3000"+imageUrl} alt="avatar" style={
  15. { width: '100%' }} /> : uploadButton}
  16. </Upload>
  17. 3、设置上传回调
  18. const handleChange = info => {
  19. if (info.file.status === 'uploading') { 上传中
  20. 进行图片上传加载动画等操作
  21. return;
  22. }
  23. if (info.file.status === 'done') { 上传成功
  24. 在这里进行设置预览图片地址等操作
  25. info.file.response.info为图片在服务器的地址,使用时需要在前面加上服务器地址
  26. }
  27. };

代码示例:

  1. import React,{ useState,useEffect} from 'react'
  2. import { Form,Card,Input,Button, message,Upload,uploadButton} from 'antd'
  3. import { createApi,getOneById,modifyOne} from '../../../utils/auth'
  4. function Edit(props) {
  5. const { getFieldDecorator}=props.form
  6. const [name,setName]=useState('');
  7. const [price,setPrice]=useState(0);
  8. const [imageUrl,setIamgeUrl]=useState('');
  9. const [loading,setLoading]=useState(false);
  10. // var name='';
  11. // var price="";
  12. const uploadButton = (
  13. <div>
  14. { loading ? 'loading' : "plus"}
  15. <div className="ant-upload-text">Upload</div>
  16. </div>
  17. );
  18. const handleChange = info => {
  19. if (info.file.status === 'uploading') {
  20. setLoading(true);
  21. return;
  22. }
  23. if (info.file.status === 'done') { //上传成功
  24. setLoading(false);
  25. console.log(info);
  26. setIamgeUrl(info.file.response.info);
  27. }
  28. };
  29. useEffect(()=>{
  30. if(props.match.params.id)
  31. {
  32. getOneById(props.match.params.id)
  33. .then(res=>{
  34. console.log(res);
  35. setName(res.name);
  36. setPrice(res.price);
  37. setIamgeUrl(res.coverImg);
  38. })
  39. }
  40. },[])
  41. const submit=(e)=>
  42. {
  43. e.preventDefault();
  44. console.log(e);
  45. props.form.validateFieldsAndScroll((err,value)=>{
  46. if(!err)
  47. {
  48. console.log(value);
  49. //修改
  50. if(props.match.params.id)
  51. {
  52. modifyOne(props.match.params.id,{ ...value,coverImg:imageUrl});
  53. props.history.push('/admin/products');
  54. }else{
  55. //添加数据
  56. createApi({ ...value,coverImg:imageUrl}).then(res=>{
  57. console.log(res);
  58. props.history.push('/admin/products');
  59. })
  60. }
  61. }else{
  62. message.error('请输入正确的内容');
  63. }
  64. })
  65. }
  66. return (
  67. <Card title='商品编辑'>
  68. <Form onSubmit={ e=>submit(e)}>
  69. <Form.Item label='名字'>
  70. {
  71. getFieldDecorator('name',{
  72. initialValue: name,
  73. rules:[{
  74. required:true,
  75. message:'请输入商品名'
  76. }]
  77. })(<Input placeholder='请输入商品名称'></Input>)
  78. }
  79. </Form.Item>
  80. <Form.Item label='价格'>
  81. {
  82. getFieldDecorator('price',{
  83. initialValue: price,
  84. rules:[{
  85. required:true,
  86. message:'请输入商品价格'
  87. }]
  88. })(<Input placeholder='请输入商品价格' ></Input>)
  89. }
  90. </Form.Item>
  91. <Form.Item label='主图'>
  92. <Upload
  93. name="file"
  94. listType="picture-card"
  95. className="avatar-uploader"
  96. showUploadList={ false}
  97. action="http://localhost:3009/api/v1/common/file_upload"
  98. onChange={ (info)=>{ handleChange(info)}}
  99. >
  100. { imageUrl ? <img src={ "http://localhost:3009"+imageUrl} alt="avatar" style={ { width: '100%' }} /> : uploadButton}
  101. </Upload>
  102. </Form.Item>
  103. <Form.Item><Button htmlType='submit' type='primary'>保存</Button></Form.Item>
  104. </Form>
  105. </Card>
  106. )
  107. }
  108. export default Form.create({ name:'productEdit'})(Edit)

发表评论

表情:
评论列表 (有 0 条评论,874人围观)

还没有评论,来说两句吧...

相关阅读

    相关 图片实现

    闲话日常: 有时候在项目中,可能会用到图片上传的预览功能,而之前总是在网上找相关的插件进行实现,但是有的时候发现别人写好的插件,有时候会影响到自身布局的样式,偶尔还很难进