【发布时间】:2021-09-15 15:59:05
【问题描述】:
我正在尝试将这些字段从 react 提交到节点服务器,我可以提交其余的输入字段并选择选项,但我无法上传照片,而且我可以使用 POSTMAN 将所有字段(包括照片)上传到服务器
// fields to submit to node server
state = {
data: {
fullName: '',
email: '',
phoneNumber: '',
branchId: '',
jobId: '',
salary: '',
image: '',
// startDate: '',
},
// onChange handler
handleChange = ({ currentTarget: input }) => {
//clone the errors
const errors = { ...this.state.errors }
//validate the input
const errorMessage = this.validateProperty(input)
//
if (errorMessage) errors[input.name] = errorMessage
//
else delete errors[input.name]
//clone the data
const data = { ...this.state.data }
//override the state data with the input value
data[input.name] = input.value
// update the state with the data and errors
this.setState({ data, errors })
}
// submit or onClick handler
doSubmit = async () => {
const { data } = this.state
const fd = new FormData()
fd.append('fullName', data.fullName)
fd.append('email', data.email)
fd.append('phoneNumber', data.phoneNumber)
fd.append('branchId', data.branchId)
fd.append('jobId', data.jobId)
fd.append('salary', data.salary)
fd.append('image', data.image)
// fd.append()
await saveEmployee(fd)
this.props.history.push('/employees')
}
【问题讨论】: