1

handle submit function:

const handleSubmit = async (acceptedFiles) => {
    const formData = new FormData();
    formData.append("file", file);

    try {
      const response = await axios.post(
        "http://127.0.0.1:8000/upload",
        formData,
        {
          headers: {
            Accept: "application/solidarity",
            "Content-Type": "multipart/form-data",
          },
        }
      );
      if (response.status >= 200 && response.status < 300) {
        console.log(response.data);
        // Handle the response, maybe display the audit results
      } else {
        throw new Error("Server responded with a non-2xx status code");
      }
    } catch (error) {
      console.error(
        "Error uploading file:",
        error.response?.data || error.message
      );
    }
  };

function in python for fastAPI

@app.post("/upload")
async def upload_file(file: UploadFile):

    data = await file.read()
    save_to = UPLOAD_DIR / file.filename
    with open(save_to, 'wb') as f:
        f.write(data)

    return {"filenames": file.filename}

I'm making a web app in react that audits files, I need it to audit files that are uploaded onto a dropbox.

I've tried checking with different file formats, without using axios and changing the input of fileupload

6
  • don't set Content-Type, because it does not contain 'boundary' value which is a must when using multipart/formdata. Remove it and axios will automatically add 'multipart' as content-type along with boundary value. scroll to see warning Commented Sep 29, 2023 at 16:13
  • 1
    Try checkin this one stackoverflow.com/questions/63048825/… Commented Sep 29, 2023 at 16:14
  • Does this answer your question? Commented Sep 29, 2023 at 16:42
  • Please have a look at this answer as well. You may also find this helpful. Commented Sep 29, 2023 at 16:43
  • @bogdanoff it does not make a difference when I change content type Commented Sep 30, 2023 at 0:34

0

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.