简体   繁体   English

react.js中ant design的上传组件(上传xls、xlsx文件)的使用方法

[英]How to use upload component (upload xls, xlsx file) of ant design in react.js

I want to use the upload component of ant design, but I don't understand how the props of this component work.想用ant design的上传组件,但是不明白这个组件的props是怎么用的。

I wrote this code, but it doesn't work我写了这段代码,但它不起作用

<Upload name="file" accept=".xls, .xlsx">
 <Button icon={<UploadOutlined/>}>Click to upload</Button>
</Upload>

在此处输入图像描述

There are two ways of using this component.有两种使用此组件的方法。


First第一的

When the file is selected, the component automatically sends this file to the endpoint has been set as the value of action property (There should be a server to get and store this file when the endpoint is called).选择文件后,组件自动将此文件发送到已设置为action属性值的端点(调用端点时应该有一个服务器来获取和存储此文件)。 In this situation you often should set headers (like Authorization header) for the Restful request that is being sent to the server as an API.在这种情况下,您通常应该为作为 API 发送到服务器的 Restful 请求设置标头(如Authorization标头)。

import { UploadOutlined } from '@ant-design/icons';
import { Button, message, Upload } from 'antd';
import { UploadChangeParam, UploadFile } from 'antd/es/upload';

function Test() {

  const uploadChanged = (event: UploadChangeParam<UploadFile<any>>) => {
    if (event.file.status !== 'uploading') {
      console.log(event.file, event.fileList);
    }
    if (event.file.status === 'done') {
      message.success(`${event.file.name} file uploaded successfully`);
    } else if (event.file.status === 'error') {
      message.error(`${event.file.name} file upload failed.`);
    }
  }

  return (
    <Upload
      name="file"
      action="https://EndPointForUploadingTheFile"
      headers={{
        authorization: 'the authorization token from the user',  //if needed
      }}
      accept=".xls, .xlsx"
      onChange={e => uploadChanged(e)}
    >
      <Button icon={<UploadOutlined />}>Click to Upload</Button>
    </Upload>
  );
}

Notice that, with the help of uploadChanged callback function, you can check whether the file was uploaded or is being uploaded or the API has been failed.注意,借助uploadChanged回调函数,您可以检查文件是否已上传或正在上传或 API 已失败。


Second第二

You want to stop the automatic file uploader API, and store the uploaded files in a state until the form you are filling out is finished.您想停止自动文件上传器 API,并将上传的文件存储在一个状态中,直到您填写的表格完成。 Then send the whole form content (including these files) via the submit API to the server.然后通过提交 API 将整个表单内容(包括这些文件)发送到服务器。 You can do it by passing a callback which returns false through beforeUpload property like so:您可以通过beforeUpload属性传递一个返回false的回调来做到这一点,如下所示:

import { UploadOutlined } from '@ant-design/icons';
import { Button, Upload } from 'antd';
import { UploadChangeParam, UploadFile } from 'antd/es/upload';
import { useState } from 'react'

function Test() {

  const [files, setFiles] = useState<UploadFile<any>[]>([])

  const uploadChanged = (event: UploadChangeParam<UploadFile<any>>) => {
     setFiles(event.fileList);
  }

  const fileRemoved = (event: UploadFile<any>) => {
    const filteredFiles = files.filter(file => file !== event);
    setFiles(filteredFiles);    
  }

  return (
    <Upload
      name="file"
      showUploadList={{ showRemoveIcon: true }}
      accept=".xls, .xlsx"
      beforeUpload={() => false}
      onChange={e => uploadChanged(e)}
      onRemove={e => fileRemoved(e)}
    >
      <Button icon={<UploadOutlined />}>Click to Upload</Button>
    </Upload>
  );
}

PS : When the Upload component content is changed, the list of the uploaded files in Upload is stored in files state (by uploadChanged callback). PS :当Upload组件内容发生变化时, Upload中上传的文件列表保存在files状态中(通过uploadChanged回调)。 Also when one of the files in Upload is removed, fileRemoved callback omits that file from the files state to keep the files and Upload content the same.此外,当Upload中的一个文件被删除时, fileRemoved回调会从files状态中忽略该文件,以保持filesUpload内容相同。

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

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