I am trying to upload multiple files by using Flask, HTML and JS. However, when I try to retrieve their names in the route of Flask the list if empty.
This is the flask route (named upload):
@app.route("/upload", methods=["POST", "GET"])
def upload():
if request.method == "POST":
return str(request.files)
Then in mu upload.html I have the following script:
<form action="" method="POST" enctype="multipart/form-data">
<div style="padding-left: 7%">
<div class="file-upload">
<input class="file-upload__input" type="file" name="files" id="files" multiple>
<button class="file-upload__button" type="button">Choose File(s)</button>
<span class="file-upload__label" style="color: white">No file(s) selected</span>
</div>
<script>
Array.prototype.forEach.call(
document.querySelectorAll(".file-upload__button"),
function(button) {
const hiddenInput = button.parentElement.querySelector(
".file-upload__input"
);
const label = button.parentElement.querySelector(".file-upload__label");
const defaultLabelText = "No file(s) selected";
// Set default text for label
label.textContent = defaultLabelText;
label.title = defaultLabelText;
button.addEventListener("click", function() {
hiddenInput.click();
});
hiddenInput.addEventListener("change", function() {
const filenameList = Array.prototype.map.call(hiddenInput.files, function(
file
) {
return file.name;
});
label.textContent = filenameList.join(", ") || defaultLabelText;
label.title = label.textContent;
});
}
);
</script>
<div style="padding-top: 75px; padding-left: 7%; padding-bottom: 10%">
<input type="submit" value="Save to Cloud" name="submit" class="file-upload__button">
</div >
</form>
When I launch it and the method is POST I have the following output :
How can I have access to the name of the files I uploaded please ?
This is what my interface looks like :
UPDATE:
When I do print(request.files)
before the return
I get :
ImmutableMultiDict([('files', <FileStorage: 'test.csv' ('text/csv')>)])
where test.csv
is indeed the correct name of the file.
Please let me know if you have any ideas of what I am doing wrong