I am trying to receive more than one file per time using reactjs and flask api, but I get this error
JavaScript
x
3
1
data = file.read()
2
AttributeError: 'str' object has no attribute 'read'
3
fontend code using reactjs
JavaScript
1
22
22
1
this.state = {
2
file : []
3
}
4
5
fileSelectHandler = (event) => {
6
var totalfiles = event.target.files.length;
7
for (var index=0; index < totalfiles; index++){
8
this.state.file.push(event.target.files[index])
9
}
10
}
11
12
async onFormSubmit (event) {
13
event.preventDefault();
14
const formData = new FormData();
15
formData.append("file", this.state.file);
16
17
await fetch(apiUrl+'photo/create' , {
18
method: 'POST',
19
body: formData
20
})
21
}
22
backend code using flask
JavaScript
1
8
1
@app.route('/photo/create', methods=['POST'])
2
def create_photo():
3
files = request.form.getlist("file")
4
print(files)
5
6
for file in files:
7
data = file.read()
8
flask receiving the files as [‘[object File],[object File]’] .
I tried to find ways to read an object file but nothing worked can anyone help..
Advertisement
Answer
I don’t use React
but for me problem makes
JavaScript
1
2
1
formData.append("file", this.state.file);
2
because this.state.file
is list with one or many files and it needs for
-loop to add every file as separated object in Form
JavaScript
1
8
1
const formData = new FormData();
2
3
var totalfiles = this.state.file.length;
4
5
for (var index=0; index < totalfiles; index++){
6
formData.append("file", this.state.file[index]);
7
}
8
And now it sends all files in request.files
instead of one (useless) string '[object File],[object File]'
in request.form
Minimal working code
JavaScript
1
61
61
1
from flask import Flask, request, render_template_string
2
3
app = Flask(__name__)
4
5
@app.route('/', methods=['GET', 'POST'])
6
def index():
7
return render_template_string('''
8
<!DOCTYPE html>
9
10
<script>
11
this.state = {
12
file: []
13
}
14
15
fileSelectHandler = (event) => {
16
console.log("fileSelectHandler");
17
var totalfiles = event.target.files.length;
18
for (var index=0; index < totalfiles; index++){
19
this.state.file.push(event.target.files[index]);
20
}
21
}
22
23
async function onFormSubmit(event) {
24
console.log("onFormSubmit");
25
event.preventDefault();
26
27
const formData = new FormData();
28
29
var totalfiles = this.state.file.length;
30
for (var index=0; index < totalfiles; index++){
31
formData.append("file", this.state.file[index]);
32
}
33
34
// doesn't need `http://.../` if sends to the same server
35
await fetch('/photo/create',
36
{
37
method: 'POST',
38
body: formData,
39
});
40
}
41
</script>
42
43
<input type="file" multiple onChange="fileSelectHandler(event);">
44
<button type="submit" onClick="onFormSubmit(event);">Submit</button>
45
''')
46
47
@app.route('/photo/create', methods=['GET', 'POST'])
48
def photo():
49
print('args :', request.args)
50
print('form :', request.form)
51
print('json :', request.json)
52
print('files:', request.files)
53
for file in request.files.getlist('file'):
54
print(file.filename)
55
#print(file.read())
56
return render_template_string(''' ''')
57
58
if __name__ == '__main__':
59
#app.debug = True
60
app.run()
61