javascript - react 问题 : I tried file data set in state when upload file, 但它不起作用

标签 javascript reactjs file multipartform-data form-data

首先,我不是一个以英语为母语的人。因此,我的句子可能在语法上是错误的,或者可能难以传达含义。

我有一个使用 Laravel 和 Axios 的 React 项目。我想将图像数据上传到我的服务器上。因此,我尝试将文件上传时的状态设置为遵循 this document ,但它不起作用。

我想知道它不起作用的原因。

这是我的一些代码。

import Axios from 'axios';
import React, { Component } from 'react';

class Register extends Component {
  constructor() {
    super();
    this.state = {
      name: '',
      email: '',
      password: '',
      password_confirmation: '',
      profile_picture: null,
      errors: [],
    }

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.hasErrorFor = this.hasErrorFor.bind(this);
    this.renderErrorFor = this.renderErrorFor.bind(this);
  }

  handleChange(event) {
    if(event.target.name.match('profile_picture')) {
      // when input profile picture
      this.setState({
        [event.target.name]: event.target.files[0],
      });

      console.log(event.target.files[0]);
    } else {
      // when input text values
      this.setState({
        [event.target.name]: event.target.value,
      });
    }

    console.log(this.state);
  }

  handleSubmit(event) {
    // submit event
  }

  hasErrorFor(field) {
    // detect error
  }

  renderErrorFor(field) {
    // render error message
  }

  render() {  
    return (
      <div className="container">
        <div className="row justify-content-center">
          <div className="col-md-8">
            <div className="card">
              <div className="card-header">Register</div>
              <div className="card-body">
                <form 
                  action="/api/register"
                  method="post"
                  encType='application/x-www-form-urlencoded'
                  onSubmit={this.handleSubmit}
                >
                  <div className="form-group">
                    // input name
                  </div>

                  <div className="form-group">
                    // input email
                  </div>

                  <div className="form-group">
                    // input password
                  </div>

                  <div className="form-group">
                    // input password confirmation
                  </div>

                  <div className="form-group">
                    <label htmlFor="profile_picture">Profile Picture</label>
                    <input
                      id="profile_picture" 
                      type="file"
                      name="profile_picture"
                      className='form-control-file'                      
                      onChange={this.handleChange}
                    />
                    {this.renderErrorFor('profile_picture')}
                  </div>

                  <button className="btn btn-primary">SUBMIT</button>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

export default Register;

And here is result of this code.

除了上传文件时设置了状态,但文件未设置状态。

最佳答案

您在 setState 方法之后立即编写了 console.log() ,因为 setState 是一个异步方法,当 java 脚本运行您的 console.log 时,它将向您显示之前的状态,其中显然未设置图像对象,为此,您应该将控制台放在 setState 的第二个参数中,它是一个回调函数。

this.setState({[event.target.name]: event.target.files[0]},()=>console.log(this.state));

关于javascript - react 问题 : I tried file data set in state when upload file, 但它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56109032/

相关文章:

c - 相对路径/绝对路径查询在当前目录中不起作用

javascript - Chrome 中的全尺寸子窗口

javascript - 在react和webpack中获取工作目录和文件名

reactjs - 使用 webpack 时如何跟踪 Reactjs 控制台错误?

Delphi:检查文件是否正在使用

java - 在 Java 中读取输入流的第一部分

当浏览器窗口的尺寸为 x 或 y 时,Javascript 会调整浏览器窗口的大小

javascript - sprockets 未正确处理 js 文件中的 erb

javascript - React : Cannot update during an existing state transition (such as within `render` ). 渲染方法应该是 props 和 state 的纯函数

javascript - 为什么 ReactJs 在部分状态改变时绘制整个组件?