【问题标题】:submitting input field and file to server.(Not working)将输入字段和文件提交到服务器。(不工作)
【发布时间】:2022-01-17 08:27:39
【问题描述】:

下面的代码有什么问题?我不能使用functional component,因为它的旧应用程序。所以我使用class。 当我点击upload 按钮console.log 打印未定义。 server 结束时什么都没有发生。

请检查handleSubmit方法好吗?

import React from 'react';
import axios from 'axios';

class FileUpload extends React.Component {
  constructor() {
    super();
    this.state = {
      selectedFile: '',
      countryCode: '',
      responseArray: [],
    };
    this.handleInputChange = this.handleInputChange.bind(this);
    this.handleInput = this.handleInput.bind(this);
  }

  handleInputChange(event) {
    this.setState({
      selectedFile: event.target.value,
      responseArray: [],
    });
  }

  handleInput(event) {
    this.setState({
      countryCode: event.target.value,
    });
  }

  handleSubmit() {
    if (!this.state.selectedFile) {
      alert('Please select The file');
      return false;
    }
    if (!this.state.countryCode) {
      alert('Please select The Country Code');
      return false;
    }
    const data = new FormData();

    for (let i = 0; i < this.state.selectedFile.length; i++) {
      data.append('file', this.state.selectedFile[i]);
    }
    data.append('countryCode', this.state.countryCode);
    console.log(data.countryCode);

    let url = process.env.API_URL;

    axios.post('http://localhost:8080/file_upload', data, {}).then(
      (res) => {
        console.log(data);
        // this.setState({ responseArray: res.data });
        // this.resetFile();
      },
      (error) => {
        alert(error);
      }
    );
  }

  resetFile() {
    document.getElementsByName('file')[0].value = null;
  }

  render() {
    return (
      <form>
        <div className="row">
          <div className="col-md-12">
            <h1>Translation File Upload</h1>

            <div className="form-row">
              <div className="form-group col-md-8">
                <label>Please enter the country code</label>
                <input
                  type="text"
                  value={this.state.countryCode}
                  onChange={this.handleInput}
                  required
                />
              </div>
            </div>

            <div className="form-row">
              <div className="form-group col-md-8">
                <label>Select File :</label>
                <input
                  type="file"
                  className="form-control"
                  multiple
                  name="file"
                  onChange={this.handleInputChange}
                  required
                />
                <hr />
              </div>
            </div>
            <br />
            <div className="form-row">
              <div className="col-md-6">
                <button onClick={this.handleSubmit.bind(this)}>Upload </button>
              </div>
            </div>
            <br />
          </div>
        </div>
      </form>
    );
  }
}

export default FileUpload;

【问题讨论】:

    标签: javascript reactjs user-interface file-upload react-router


    【解决方案1】:

    尝试将handleInputChange 更改为

    handleInputChange(event) {
        this.setState({
          selectedFile: event.target.files[0],
          responseArray: [],
        });
      }
    

    相关docs

    【讨论】:

    • 能否请您检查它是否正在向服务器发送正确的有效负载。因为当我从邮递员发送时,它工作正常。从此组件 500 错误即将到来。我也认为它没有验证正确的输入。非常感谢。
    • 还有一个文件,不是多个。请提出更改建议。我很新反应。由于项目的紧迫性。
    • 好吧,input.files 包含 FileList 并且要获取单个文件,您可以使用像 input.files[0] 这样的数组语法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 2017-07-27
    • 2015-09-19
    • 2012-01-03
    • 2017-08-21
    • 2022-08-16
    相关资源
    最近更新 更多