0

I am trying to upload file using React and passing it to flask backend using post request. But i am getting error as Failed to load resource: the server responded with a status of 400 (BAD REQUEST). I can't figure out the problem. If there is any other method possible, that also will be helpful.

This is upload.js:

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

class UploadVideo extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      imageURL: '',
    };

    this.handleUploadImage = this.handleUploadImage.bind(this);
  }

  handleUploadImage(ev) {
        ev.preventDefault();

        const data = new FormData();
        data.append('file', this.uploadInput.files[0]);
        data.append('filename', this.fileName.value);
        console.log(data)
        axios('/upload', {
            method: 'POST',
            body: data,
        }).then((response) => {
            console.log(response)
        }).catch(err => {
            console.log(err)
          });
  }

  render() {
    return (
      <form onSubmit={this.handleUploadImage}>
        <div>
          <input ref={(ref) => { this.uploadInput = ref; }} type="file" />
        </div>
        <div>
          <input ref={(ref) => { this.fileName = ref; }} type="text" placeholder="Enter the desired name of file" />
        </div>
        <br />
        <div>
          <button type="submit" className="btn">Upload</button>
        </div>
        <img src={this.state.imageURL} alt="img" />
      </form>
    );
  }

This is index.py in flask:

@app.route('/upload', methods=['POST'])
def fileUpload():

   file = request.files['file'] 
   filename = secure_filename(file.filename)
   print(request.body, '1')
   return {"message":"Saved"}
5
  • can you share your localhost url for both react and flask apps Commented Sep 22, 2020 at 15:44
  • localhost:8000/upload. Its this Commented Sep 22, 2020 at 15:47
  • so react is running on localhost:3000? Commented Sep 22, 2020 at 15:49
  • Nope, react running on localhost:3500 Commented Sep 22, 2020 at 15:52
  • Does this answer your question? file upload with Reactjs and Flask Commented Sep 25, 2023 at 18:18

1 Answer 1

0

since react and flask running in different port use full URL. check the code below

handleUploadImage(ev) {
    ev.preventDefault();

    const data = new FormData();
    data.append('file', this.uploadInput.files[0]);
    data.append('filename', this.fileName.value);
    console.log(data)
    axios('http://localhost:8000/upload', {
        method: 'POST',
        body: data,
    }).then((response) => {
        console.log(response)
    }).catch(err => {
        console.log(err)
      });
}
Sign up to request clarification or add additional context in comments.

4 Comments

It says url must start with leading slash
sorry, I thought I edited the react code, check the edit please
Now i get error as localhost:8000/upload 400 (BAD REQUEST)
Open network console in your browser and check the exact error message you are getting from your flask app for the upload request. Also check if the file object is being sent in the request body

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.