【问题标题】:php cannot read FormData that contains multiple filesphp 无法读取包含多个文件的 FormData
【发布时间】:2020-08-25 01:24:19
【问题描述】:

在 php 中,我无法访问在 $_FILES 中上传的文件,而是在 $_POST["imgs"] 中显示为 [object File],没有像 name 这样的任何属性。 我怎样才能获得在$_FILES 中访问的那些文件?

import React, { useCallback } from 'react'
import { useDropzone } from 'react-dropzone'
import axios from 'axios'

const imgAjaxUploader = axios.create({
    baseURL: 'http://localhost',
    timeout: 1000,
    headers: { 'Content-Type': 'mulipart/form-data' }
});

export default function ImgDropzone() {

    const onDrop = useCallback(acceptedFiles => {
        const formData = new FormData()
        formData.append('imgs', acceptedFiles)
        try {
            imgAjaxUploader.post('/store/admin/imgHandler.php', formData, {
                headers: {
                    'Content-Type': 'mulipart/form-data'
                }
            }).then(data =>
                console.log(data)
            ).catch(err => {
                console.log(err)
                return null
            })

        } catch (err) {
            alert(err)
        }
    }, [])


    const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop: onDrop, accept: 'image/jpeg, image/png' })

    return (
        <div {...getRootProps()} style={{ display: "inline-block" }}>
            <input {...getInputProps()} />
            {
                isDragActive ?
                    <p>Drop the files here ...</p> :
                    <p>Drag 'n' drop some files here, or click to select files</p>
            }
        </div>
    )
}

【问题讨论】:

    标签: php reactjs dropzone.js form-data react-dropzone


    【解决方案1】:

    我找到了解决方案。多个文件需要在同名后追加[],以便与PHP兼容:

        acceptedFiles.forEach(file => {
            formData.append('imgs[]', file)
        })
    

    resource 示例 3

    【讨论】:

      猜你喜欢
      • 2012-01-12
      • 2015-08-01
      • 1970-01-01
      • 2010-10-13
      • 2021-07-23
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多