简体   繁体   English

在 react js 中使用蚂蚁设计上传图片

[英]Upload an image using ant design in react js

I have a simple application where user should upload its image.我有一个简单的应用程序,用户应该在其中上传其图像。 After uploading the image, and clicking on get data from upload button, the user should see the result in console.log("Received values of form: ", values);上传图像后,点击get data from upload按钮get data from upload ,用户应该在console.log("Received values of form: ", values);看到结果console.log("Received values of form: ", values); Now, the result appears but in form of an array of items.现在,结果出现了,但以项目数组的形式出现。
What i want to achieve is, to get just the last uploaded image there, and in the same time on front end i want to see just one square with image, but now if i upload 3 images, i get 3 squares.我想要实现的是,只在那里获取最后上传的图像,同时在前端我只想看到一个带有图像的正方形,但是现在如果我上传 3 张图像,我会得到 3 个正方形。
ps: for me is obligatory to get the thumbUrl after saving. ps:对我来说, thumbUrl后必须获得thumbUrl There is base64 data of image.有图像的base64数据。

 import React, { useState } from "react"; import ReactDOM from "react-dom"; import "antd/dist/antd.css"; import "./index.css"; import { Form, Select, InputNumber, Switch, Radio, Slider, Button, Upload, Rate, Checkbox, Row, Col } from "antd"; import { UploadOutlined, InboxOutlined } from "@ant-design/icons"; const { Option } = Select; const formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 14 } }; const normFile = (e) => { console.log("Upload event:", e); if (Array.isArray(e)) { return e; } return e && e.fileList; }; const Demo = () => { const onFinish = (values) => { console.log("Received values of form: ", values); }; const [state, setState] = useState({ loading: false }); function getBase64(img, callback) { const reader = new FileReader(); reader.addEventListener("load", () => callback(reader.result)); reader.readAsDataURL(img); } const handleChange = (info) => { let fileList = [...info.fileList]; if (info.file.status === "uploading") { setState({ loading: true }); return; } if (info.file.status === "done") { // Get this url from response in real world. getBase64(info.file.originFileObj, (imageUrl) => setState({ imageUrl, loading: false }) ); } }; function beforeUpload(file) { const isJpgOrPng = file.type === "image/jpeg" || file.type === "image/png"; if (!isJpgOrPng) { console.error("You can only upload JPG/PNG file!"); } const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { console.error("Image must smaller than 2MB!"); } return isJpgOrPng && isLt2M; } const uploadButton = ( <div> <div className="ant-upload-text">Upload img</div> </div> ); const { imageUrl } = state; return ( <Form name="validate_other" {...formItemLayout} onFinish={onFinish} initialValues={{ "input-number": 3, "checkbox-group": ["A", "B"], rate: 3.5 }} > <Form.Item name="upload" label="Upload" valuePropName="fileList" getValueFromEvent={normFile} > <Upload listType="picture-card" className="avatar-uploader" showUploadList={true} action="https://www.mocky.io/v2/5cc8019d300000980a055e76" beforeUpload={beforeUpload} onChange={handleChange} > {imageUrl ? ( <img src={imageUrl} alt="avatar" style={{ width: "100%" }} /> ) : ( uploadButton )} </Upload> </Form.Item> <Form.Item wrapperCol={{ span: 12, offset: 6 }} > <Button type="primary" htmlType="submit"> get data from upload </Button> </Form.Item> </Form> ); }; ReactDOM.render(<Demo />, document.getElementById("container"));

Question: How to achieve what i described above?问题:如何实现我上面描述的?
demo: https://codesandbox.io/s/currying-pine-hq412?file=/index.js:0-2985演示: https : //codesandbox.io/s/currying-pine-hq412?file=/ index.js: 0-2985

You can gain full control over the uploaded files by configuring fileList (see Antd Upload Api for fileList).您可以通过配置fileList来完全控制上传的文件(请参阅Antd Upload Api for fileList)。

fileList文件列表
List of files that have been uploaded (controlled).已上传(受控)的文件列表。

A demo how to use the fileList and reduce the list size is provided in the docs too.文档中也提供了如何使用 fileList和减小列表大小的演示。

Note: There might be some issues while using fileList , see this(#2423) ant github issue for details.注意:使用fileList可能会出现一些问题,请参阅此(#2423) ant github issue 了解详细信息。

You just need to notify the form to get the last item in the fileList array, through getValueFromEvent您只需要通过getValueFromEvent通知表单获取 fileList 数组中的最后一项

const normFile = (e) => {
  console.log("Upload event:", e);

  if (Array.isArray(e)) {
    return e;
  }

  //change "return e && e.fileList" for
  return e && e.fileList.slice(-1);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM