Antd和React使用Upload上传组件自定义上传文件(图片为例)

1.定义结构

             <Upload
                accept="image/*"
                listType="picture-card"
                fileList={fileList}
                onChange={onChange}
                onPreview={onPreview}
                customRequest={handlePreview}
              >
                {fileList.length < 1 && "+ 上传封面"}
              </Upload>

2.内容

import type { GetProp, UploadFile, UploadProps } from "antd";

const [fileList, setFileList] = useState<UploadFile[]>([]);

const onChange: UploadProps["onChange"] = ({ fileList: newFileList }) => {
    setFileList(newFileList);
  };

const onPreview = async (file: UploadFile) => {
    let src = file.url as string;
    if (!src) {
      src = await new Promise((resolve) => {
        const reader = new FileReader();
        reader.readAsDataURL(file.originFileObj as FileType);
        reader.onload = () => resolve(reader.result as string);
      });
    }
    const image = new Image();
    image.src = src;
    const imgWindow = window.open(src);
    imgWindow?.document.write(image.outerHTML);
  };

const handlePreview = async (file: any) => {
    const form = new FormData();
    form.append("file", file.file);
    const res: any = await UploadApi(form);
    console.log("上传结果", res);
    if (res.status === 0) {
      const FileList = [
        {
          uid: "1",
          name: res.name,
          url: res.url,
        },
      ];
      setFileList(FileList);
    }
    return false;
  };

3.nodejs接收(app.js配置)

const multer = require("multer");
let objMulter = multer({ dest: "./public/upload" });
//实例化multer,传递的参数对象,dest表示上传文件的存储路径
app.use(objMulter.any()); //any表示任意类型的文件
// app.use(objMulter.image())//仅允许上传图片类型
app.use(express.static("./public"));

4.定义接口

const fs = require("fs");
async function UploadFile(req, res) {
  console.log("上传内容", req.files);
  const name = Buffer.from(req.files[0].originalname, "latin1").toString(
    "utf8"
  );//nodejs对于中文产生乱码的解决办法
  let oldName = req.files[0].path; //获取名字
  //给新名字加上原来的后缀
  let newName = req.files[0].path + name;
  fs.renameSync(oldName, newName); //改图片的名字
  res.send({
    status: 0,
    name: name,
    url: "http://localhost:6789/upload/" + req.files[0].filename + name, //该图片的预览路径
  });
}

antd 是一个基于 Ant Design 设计语言的 React UI 组件库,它提供了一套丰富的组件来帮助开发者快速构建高质量的 Web 应用。如果你想改写 antd 中的 Upload 组件的样式,可以通过以下几种方法实现: 1. **使用 className 或者 classNamePrefix:** antd组件都支持通过 className 属性添加自定义的样式类,或者使用 classNamePrefix 来为内部所有样式类添加一个前缀,从而实现样式的覆盖。如: ```jsx <Upload className="custom-upload" // 或者 // classNamePrefix="custom-upload" > <Button>点击上传</Button> </Upload> ``` 然后在你的 CSS 文件中: ```css .custom-upload .ant-upload { /* 自定义上传区域的样式 */ } ``` 2. **使用 CSS-in-JS:** 如果你使用 CSS-in-JS 的方式来编写样式,可以直接在组件使用样式对象来定义样式。使用 styled-components 或 emotion 这类库: ```jsx import styled from 'styled-components'; const StyledUpload = styled(Upload)` .ant-upload { /* 自定义样式 */ } `; <StyledUpload> <Button>点击上传</Button> </StyledUpload> ``` 3. **修改 Less 变量:** antd 默认使用 Less 作为样式预处理器,它提供了一套默认的主题变量。你可以通过覆写这些变量来全局改变组件的样式。具体方法是创建一个自定义的主题样式文件,并通过配置导入它: ```less // custom-antd.less @import '~antd/dist/antd.less'; // 引入 antd 默认样式 // 自定义 Upload 样式 .ant-upload { .ant-upload-select { /* 自定义上传按钮的样式 */ } } ``` 然后在你的应用中配置: ```jsx // 引入自定义样式文件 import './custom-antd.less'; ``` 4. **直接修改组件内部 CSS:** 直接通过 CSS 选择器找到 Upload 组件的内部元素并进行样式覆盖。这种方法虽然直接,但不推荐使用,因为它违背了封装原则,可能会在未来的版本中因组件内部实现的改变而导致样式失效。 ```css .ant-upload.ant-upload-select { /* 自定义上传按钮的样式 */ } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值