【问题标题】:In javascript, how to user inputs save to a file to a local directory在javascript中,如何将用户输入保存到本地目录的文件中
【发布时间】: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


【解决方案1】:

您必须在后端使用强大的或 multer。或者,您可以在线创建一个存储桶,并创建一个post请求将图像存储在该桶中。

【讨论】:

  • 如果你使用 expo,你也可以使用 expo 的 FileSystem API
猜你喜欢
  • 1970-01-01
  • 2019-09-13
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
  • 2015-10-25
  • 2016-03-15
  • 1970-01-01
相关资源
最近更新 更多