I am trying to upload files along with its additional field using react.js and python(flask). User can upload multiple files.
For add more functionality I am pushing Name and File in an Array and that array is stored in component's state like below.
this.state = {
files:[{name:'', file:''}]
}
And I created a rest api in flask to upload files and save data. and passing this.state
data as formData()
. And I am trying to get uploaded files in flask like below.
let formData = this.state;
const fd = new FormData();
for(let k in formData){
fd.append(k,formData[k]);
};
Rest API-
@mod_upload.route('/upload',methods=['POST'])
def upload():
if request.method == 'POST':
f = request.files['files']
print(f)
Everything is working fine but in above code f
is always empty because in formData()
I am appending this.state.files
which is an Array but it expect file Object.
So, I need suggestions, how to access files and fields value in request.files['files']
.
Please help me. Thanks
Try this. Notice the 'files'
key while appending form data. Let me know if it works
UPDATED ANS
let formData = this.state;
const fd = new FormData();
for(const k of formData){
fd.append('files',k.file, k.name);
};