antd5:图片上传接口参数

本文介绍了如何在umi4和antd5项目中使用axios进行文件上传,涉及接口/api/file/upload的POST请求,设置了content-type头,并演示了如何在app.ts文件中配置请求拦截器来处理上传参数和设置Content-Type。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用场景,后端给了一个上传文件的接口/api/file/upload,方法类型为post,参数为{file: binary,type:string}.
上传文件需要设置请求头content-type.
在这里插入图片描述

项目使用的是umi4+antd5
请求拦截便在app.ts文件写。

export const request: RequestConfig = {
  baseURL: '/api',
  timeout: 5000,
  // other axios options you want
  errorConfig: {

    errorHandler () {
    },
    errorThrower () { },
  },
  requestInterceptors: [
    (url, options) => {
      if (options.contentType) {
      //修改请求头
        options.headers['Content-Type'] = options.contentType;
      }
      return { url, options };
    }
  ],
  responseInterceptors: [],
};

上传参数示意
在这里插入图片描述

接口函数

import { request } from '@umijs/max';
export const fileUpload = (
  params: any,
  options?: { [key: string]: any },
) => request(
  '/file/upload',
  {
    method: 'POST',
    data: params,
    ...(options || {}),
    contentType: 'multipart/form-data'
  },
);
  console.log(file, 'file');
  cosUpload({
    file: file.originFileObj,//文件对象
    type: 'application'
  }).then((res) => {
    res?.data && setImageUrl(res.data);
  }).finally(() => {
    setLoading(false);
  });

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值