【发布时间】:2018-06-10 19:53:53
【问题描述】:
我决定将文件存储在本地项目中。我希望当用户点击提交时,文件将被保存到一个路径,并且我会将文件名的 Post 请求发送到一个目录。如何将文件对象保存到本地目录?
export default class Example extends React.Component {
constructor (props) {
super(props);
this.fileInput;
this.handleFormSubmit = this.handleFormSubmit.bind(this);
}
handleFormSubmit(e) {
e.preventDefault()
path = "../../images"
// Save this.fileInput to path directory
}
render() {
function FieldGroup({ id, label, help, ...props }) {
return (
<FormGroup controlId={id}>
<ControlLabel>{label}</ControlLabel>
<FormControl {...props} />
</FormGroup>
);
}
return (
<Form>
<FieldGroup
id="formControlsText"
type="text"
label="title"
placeholder="Enter text"
/>
<FieldGroup
id="formControlsFile"
type="file"
label="Image"
help="Example block-level help text here."
inputRef={ref => {this.fileInput = ref}}
/>
<Button> Submit </Button>
</Form>
);
}
}
【问题讨论】:
-
您不能使用客户端 js 执行此操作,您需要实现服务器端代码以将文件保存在服务器上,尽管您可以选择下载文件或将数据保存在客户端的本地存储中。
-
你可以参考this教程
标签: javascript node.js reactjs file