在reactjs中使用axios post发布多个文件

时间:2020-04-20 15:58:47

标签: reactjs file-upload axios

我正在尝试使用axios post发布多个文件,但后端似乎仅接收第一个文件,而不接收第二个文件。我可以在请求的表单数据中看到第二个文件。不确定出什么问题

//data

    let params = new FormData();
    this.state.file.map((file, index) => {
                    params.append(`${file.name}`, file.data);
                });

// async post
export async function createSecurityExemption(values, callback){
    const config = {
        headers: {
            'content-type': 'multipart/form-data'
        }
    }
    const request = await axios.post(`/api/admin/add/exemptions/format/json&quiet=1`,values, config)
        .then((response) => callback(response));
    return {
        type: CREATE_SECURITY_EXEMPTION,
        payload: request
    };
}

请求标头

Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Authorization: Basic dWRkaW5zMjp1ZGRpbnMy
Connection: keep-alive
Content-Length: 505462
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarywiylMwi6U0cbhBXA
Cookie: _ga=GA1.2.1766216833.1581511385; _gid=GA1.2.351822429.1587389224; PHPSESSID=75bf56d8d5e3b894b4c5b2dbc4b92986
DNT: 1
Host: ccp-dev1.domain.com
Origin: https://ccp-dev1.domain.com
Referer: https://ccp-dev1.domain.com/security/exemptions/new/135519
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36

表单数据

hld: (binary)
file2: (binary)

这里是sandox https://codesandbox.io/s/upload-multiple-files-vn4yn?file=/Todo.js

我的后端是PhP,当我使用$_FILES打印时,它仅显示第一个文件。第二个文件永远不会显示

//print_r($_FILES);die();
(
    [hld] => Array
        (
            [name] => KID 2019.pdf
            [type] => application/pdf
            [tmp_name] => /tmp/phpU0fFwp
            [error] => 0
            [size] => 251913
        )

)

0 个答案:

没有答案