I am transitioning from http.server to Flask. My image upload using AJAX is now broken. This is running Python 3.
Troubleshooting that did not work:
I have included multipart/form-data in the Ajax request.
I have tried to have a shared and dedicated route for the upload.
I have added @cross_origin() it solved the problem for a similar question.
I tried looking in other request dicts, nothing contains anything.
I originally ran the app on 0.0.0.0, so changed that to 127.0.0.1.
I tried to add an entire form to the FormData and just the image. I am reluctant to change much more of the Ajax since it works on http.server.
Tried both sync and async AJAX requests.
No matter what I attempt I always get the same result:
print(request.files)
returns ImmutableMultiDict([])
I'ld rather avoid using JQuery, as this should work since it works on http.server.
Relevant code:
Non-working Flask:
@app.route("/qr_upload", methods=["GET", "POST"])
@cross_origin()
def receive_image():
if (request.method == "POST"):
print("qr_code" in request.files) # This always returns False.
multipart_data = request.files["qr_code"]
return "Post"
if __name__ == "__main__":
app.run("127.0.0.1", PORT, True)
Working AJAX on http.server
// Add the image to the request and send it.
var formData = new FormData(document.getElementById("qrPickerForm"))
xhttp.open("POST", "/qr_upload")
xhttp.setRequestHeader("Content-Type", "multipart/form-data")
xhttp.send(formData)
Related HTML
<form action = "/qr_upload" id = "qrPickerForm" name = "qr_form" method="post" enctype = "multipart/form-data">
<input id = "qrFilePicker" name = "qr_code" type = "file" accept="image/*" capture="camera">
<input type="submit">
</form>
Working http.server:
def do_POST(self):
# Extract the multiform data from the POST request
cLen = int(self.headers["Content-Length"])
body = self.rfile.read(cLen)
# Decode the multiform data and get the image bytes.
multipart_data = decoder.MultipartDecoder(body, "multipart/form-data; boundary=WebKitFormBoundary")
# This and Flask's code should have the same data here.
Handler = S
with socketserver.TCPServer(("", PORT-1), Handler) as httpd:
httpd.serve_forever()
Update 1:
It works if I send the image with a form's submit button, but not with AJAX. Have not figured out why yet though.