aiyoudiao aiyoudiao
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档

码二

扫微信二维码,认识一下码二吧😉。
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档
  • 流年往事

  • 经验片段

    • 2022年2月21日
    • 2022年3月16日
    • 2022年3月21日
    • 2022年3月23日
    • 2022年4月9日
    • 2022年4月10日
    • 2022年4月15日
    • 2022年4月27日
    • 2022年4月28日
    • 2022年5月6日
    • 2022年5月16日
    • 2022年5月20日
    • 2022年6月16日
    • 2022年6月22日
    • 2022年6月23日
    • 2022年8月17日
    • 2022年10月12日
    • 2022年11月6日
    • 2022年11月8日
    • 2022年11月21日
    • 2022年12月06日
    • 2022年12月09日
    • 2022年12月27日
    • 2023年01月21日
    • 2023年02月03日
    • 2023年03月19日
    • 2023年03月26日
    • 2023年04月12日
    • 2023年05月03日
    • 2023年06月04日
  • 读书杂感

  • 历程
  • 经验片段
aiyoudiao
2022-06-16

2022年6月16日

今天联调文件上传和下载,遇到两个坑,这个或许与使用的请求框架有关,我这里使用的是umi-request。

在umiRequest中,如果你想在请求的响应中获取response,那么就需要在参数中设置一个配置,getResponse。
而且它的header是map,不能直接header['key']的方式拿值,而且你在调试的时候会发现header是个空对象,这会令你觉得后端没给你返回响应头数据或者你会认为浏览器的安全机制导致你无法访问响应头。

import request from 'umi-request';
const options = {
    responseType: 'arrayBuffer' // blob
};
const data = {
    id: 2000
}
// request.get(url, { ...options, params: data, getResponse: true })
request.post(url, { ...options, data, getResponse: true })
.then({resposne, data} => {
    // 拿到响应头部的数据
    // const disposition = response.headers?.['content-disposition']; // 这种方式不行,因为headers是一个Map结构,得使用map的get方法才行
    const disposition = response.headers?.get('content-disposition');
})

浏览器会有安全机制不允许你访问响应的头的某些数据,比如你的自定义响应头之类的数据,这是需要后端配合一下,让浏览器允许你访问这个响应头的数据。
后端设置access-control-expose-headers,有的框架默认就可以让你访问响应头的这个数据。

response.setHeader("Access-Control-Expose-Headers","Content-Disposition");// 可以添加多个,用,隔开就行,比如:Content-Disposition,Content-Type

antd 组件文件上传

antd 的upload组件默认是有上传机制的,如果你想不用它的上传机制,可以改写一下。

import {
  Button,
  Col,
  Form,
  message,
  Row,
  Upload
} from 'antd';
const [data, setData] = useState(null);

const props = {
    name: 'file',
    action: '',
    headers: {
        "content-type": "multipart/form-data"
    },
    accept: 'image/png',
    maxCount: 1,
    beforeUpload: file => {
        const isPNG = file.type === 'image/png';
        console.log('file', file);
        if (!isPNG) {
        message.error(`${file.name} is not a png file`);
        }
        return isPNG || Upload.LIST_IGNORE;
    },
    customRequest(options ) {
        const { onSuccess, file } = options;
        onSuccess();
    },
    onChange(info) {
        const fmData = new FormData();
        fmData.append('myFile', info.file.originFileObj);
        fmData.append('code', '123456789');
        setData(fmData);
    }
};
const upload = () => {
    if (data) {
        dispatch({
            type: 'model/upload',
            payload: data,
            callback: (response) => {
                if (!response.success) {
                    alert('错误:' + response.msg);
                    return;
                }
            }
        });
    }
};

return (
<Form>
    <Row gutter={24} justify="center">
        <Col span={22} style={{ textAlign: 'left' }}>
        <Form.Item name="select" label="选择">
            <Upload {...props}>
            <Button type="default">请选择文件</Button>
            </Upload>
        </Form.Item>
        </Col>
    </Row>
    <Row gutter={24} justify="center">
        <Col span={24} style={{ textAlign: 'center' }}>
        <Button style={{ margin: '0 8px', width: 200 }} type="primary" onClick={() => upload()}>
            上传
        </Button>
        </Col>
    </Row>
</Form>
)
#umi-request#HTTP#react
上次更新时间: 10年18月2023日 01时57分53秒
2022年5月20日
2022年6月22日

← 2022年5月20日 2022年6月22日 →

最近更新
01
01.数据结构导论一览.md
10-16
02
30.2023年06月04日.md
06-04
03
08.与测量相关.md
05-06
更多文章>
Theme by Vdoing | Copyright © 2017-2023 aiyoudiao 码二 备案号: 鄂ICP备2022002654号-1