Trouble uploading json file with flask

Viewed 197

For some reason, this won't accept json files.

@app.route('/get_data', methods=['POST'])
def get_data():
    dataFile = request.files['file_path']
    dataFileName = dataFile.filename
    dataFile.save(os.path.join(uploads_dir, dataFileName))

I keep getting this error:

enter image description here

1 Answers

You seem to have json set as an file ending in your template by <input type="file" accept="json">. (The template is not supplied so I can't pinpoint the line. This is not an error of the backend (flask) but of the your template code (jinja/html). It would be nice if you could supply a MRE for such issues.

For more information about <input type="file"> take a look at the MDN Documentation.

Example of correct accept:

<input type="file" accept=".json">

This will only allow *.json file but keep in mind that users may supply other files manually and create a fallback or validation when parsing/ saving the file.

Related