提问者:小点点

如何使用reactjs上传图像


我想要实现的是,当用户按下choosefile并选择一个图像文件时,js会将图像文件保存在目录中并使用它。提前感谢:)这是我的HTML:

是否有我应该执行的post请求?


共2个答案

匿名用户

安装Axios:运行以下命令。

npm install axios --save

示例:

import axios from 'axios'; 

import React,{Component} from 'react'; 

class App extends Component { 

    state = { 

    // Initially, no file is selected 
    selectedFile: null
    }; 
    
    // On file select (from the pop up) 
    onFileChange = event => { 
    
    // Update the state 
    this.setState({ selectedFile: event.target.files[0] }); 
    
    }; 
    
    // On file upload (click the upload button) 
    onFileUpload = () => { 
    
    // Create an object of formData 
    const formData = new FormData(); 
    
    // Update the formData object 
    formData.append( 
        "myFile", 
        this.state.selectedFile, 
        this.state.selectedFile.name 
    ); 
    
    // Details of the uploaded file 
    console.log(this.state.selectedFile); 
    
    // Request made to the backend api 
    // Send formData object 
    axios.post("api/uploadfile", formData); 
    }; 
    
    // File content to be displayed after 
    // file upload is complete 
    fileData = () => { 
    
    if (this.state.selectedFile) { 
        
        return ( 
        <div> 
            <h2>File Details:</h2> 
            <p>File Name: {this.state.selectedFile.name}</p> 
            <p>File Type: {this.state.selectedFile.type}</p> 
            <p> 
            Last Modified:{" "} 
            {this.state.selectedFile.lastModifiedDate.toDateString()} 
            </p> 
        </div> 
        ); 
    } else { 
        return ( 
        <div> 
            <br /> 
            <h4>Choose before Pressing the Upload button</h4> 
        </div> 
        ); 
    } 
    }; 
    
    render() { 
    
    return ( 
        <div> 
            <h1> 
            GeeksforGeeks 
            </h1> 
            <h3> 
            File Upload using React! 
            </h3> 
            <div> 
                <input type="file" onChange={this.onFileChange} /> 
                <button onClick={this.onFileUpload}> 
                Upload! 
                </button> 
            </div> 
        {this.fileData()} 
        </div> 
    ); 
    } 
} 

export default App; 

匿名用户

超文本标记语言

<form onSubmit={this.onFormSubmit}>
  <h3>Upload Profile Picture</h3>
  <input type="file" name="userPhoto" onChange={this.onChange} />
  <button type="submit">Upload</button>
</form>

JS

import { uploadPicture } from "./Actions";

onChange(e) {
  this.setState({ file: e.target.files[0] });
}
onFormSubmit(e) {
    e.preventDefault();
    const formData = new FormData();
    formData.append("usrPhoto", this.state.file);
    uploadPicture(formData)
      .then((res) => {
        console.log(res);
      });
  }

行动JS

export const uploadPicture = (formData) {
  const config = {
    headers: {
      "content-type": "multipart/form-data",
    },
  };
  return axios
    .post([YOUR APP URL] + "api/user/updatePicture", formData, config)
    .then(function (response) {
      console.log(response.data)
    })
    .catch(function (error) {
      //  some error occurred
      return [];
    });
};

中。状态文件您在服务器上有图像数据命中请求,需要保存在目录中。