Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

reactjs - React: FormData not sending image data to Flask backend

I am trying to upload an image, but the form data simply doesn't append the file data to my request.

Front end:

const form = new FormData()
form.append('files[]', file)
form.append("woor","w")
console.log(file)
const header = {
     headers: { 'Content-Type': 'multipart/form-data'}
}
axios.post(
   action,
   form,
   header
).then(({ data: response }) => {})

Flask backend:

def upload():
    print(request.form)
    print(request.json)    
    return {"body":[{"value":"fdd", "label":"fdaad"}]}

The backend prints out ImmutableMultiDict([('woor', 'w')]) but not the file, console.log does show the file data I have also tried {"file":file} and {"file[]":file}

EDIT: file is not an array. It looks like a dict to me from console.log()

question from:https://stackoverflow.com/questions/65896539/react-formdata-not-sending-image-data-to-flask-backend

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I recently came across the same Issue, the answer might be suprisingly simple, you access the file with request.files instead of request.form in flask.
Here is also a link to the SO question where I had the same Issue, figured it out after a few days: Dropzone.js not sending the file when adding additional data


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...