1

I am working on a form for uploading files from a local disk within the react component. After uploading the file, it should be sent to the server using the POST request, where it is processed using express-fileupload to upload to the server. The server responds that the request is too large:

PayloadTooLargeError: request entity too large

Here is the component:

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


class FileUpload extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sendImage: null
    };
    this.openFile = this.openFile.bind(this);
    this.sendFile = this.sendFile.bind(this);
  }
  onChange = (e) => {
    var file = e.target.files[0];
    var reader = new FileReader();
    reader.onloadend = () => {
      this.setState({ sendImage: reader.result })
    }
    reader.readAsDataURL(file);
  }
  openFile(e) {
    this.refs.fileUploader.click();
  }
  sendFile = () => {
    axios.post(this.props.API_URL + "upload/news", { data: this.state.sendImage })
      .then(res => {
      })
  }  
  render() {
    return (
      <>
        <button className="xbutton" onClick={this.sendFile} >Upload</button>
        <button className="xbutton" onClick={this.openFile} >Open</button>
        <
          input
          type="file"
          name="sampleFile"
          ref="fileUploader"
          defaultValue=""
          onChange={e => this.onChange(e)}
        />
      </>
    );
  }
}

export default FileUpload;

Please tell me what the error is, and is it possible to upload files using these tools? Thanks!

Kiten
  • 457
  • 1
  • 5
  • 19
  • 1
    Have you perhaps seen this answer? https://stackoverflow.com/questions/19917401/error-request-entity-too-large – Reez0 Sep 02 '19 at 09:11
  • After I fix that, Express responds that "TypeError: uploaded_file.mv is not a function". (line from express: uploaded_file.mv(__dirname + '/files/' + "fileName" + '.jpg' , function(err)) – Kiten Sep 02 '19 at 09:47

0 Answers0