Reactjs 试图通过POST请求传递状态时,我的请求负载为空
我正试图通过Post请求发送csv文件,而我的请求负载发送为空,我不确定为什么会发生这种情况这是我的组件:Reactjs 试图通过POST请求传递状态时,我的请求负载为空,reactjs,api,post,Reactjs,Api,Post,我正试图通过Post请求发送csv文件,而我的请求负载发送为空,我不确定为什么会发生这种情况这是我的组件: const IFImport = (props) => { const [file, setFile] = useState({}); const onUpload = (e) => { const csvFile = e.target.files; console.log(csvFile) setFile(
const IFImport = (props) => {
const [file, setFile] = useState({});
const onUpload = (e) => {
const csvFile = e.target.files;
console.log(csvFile)
setFile(...file, csvFile)
}
const handleSave = () => {
event.preventDefault();
console.log(file)
props.onSave(api.CSVImport.upload(file))
}
const handleCancel = () => {
props.onCancel();
}
return (
<>
<ContentRow>
<h1>
<Link to={"/"}>
<Button color="link"><</Button>
</Link>
Upload Enrollment Information
<ErrorList error={props.errors} />
</h1>
</ContentRow>
<hr/>
<ContentRow>
<Form onSubmit={(e) => handleSave(e)}>
<Label>Upload a CSV File for Enrollment</Label>
<FormGroup>
<div>
{file !== null ? <p>{file.name}</p> : ""}
</div>
<div>
<Input
type="file"
name="data.file"
onChange={e => onUpload(e)}
accept=".csv"
/>
</div>
</FormGroup>
<hr/>
<ButtonToolbar>
<SubmitButton inProgress={props.inProgress}>
Save Enrollment
</SubmitButton>
<Button
disabled={props.inProgress}
color="link"
onClick={() => handleCancel()}
>
Cancel
</Button>
</ButtonToolbar>
</Form>
</ContentRow>
</>
);
};
export default connect(mapStateToProps, mapDispatchToProps)(IFImport);
const IFImport=(道具)=>{
const[file,setFile]=useState({});
const onUpload=(e)=>{
const csvFile=e.target.files;
console.log(csvFile)
setFile(…文件,csvFile)
}
常量handleSave=()=>{
event.preventDefault();
console.log(文件)
onSave(api.CSVImport.upload(文件))
}
常量handleCancel=()=>{
props.onCancel();
}
返回(
上传报名信息
handleSave(e)}>
上载用于注册的CSV文件
{file!==null?{file.name}:“”
onUpload(东)}
accept=“.csv”
/>
保存注册
handleCancel()}
>
取消
);
};
导出默认连接(MapStateTrops、mapDispatchToProps)(iImport);
当我在handleSave
函数中控制台我的文件时,我得到了文件对象,但仍然以这种方式发送它是不起作用的。我还尝试通过在我的handleSave函数中执行props.onSave(api.CSVImport.upload({payload:file}))
将文件作为对象发送,但这也不起作用