简体   繁体   中英

Trouble uploading json file with flask

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:

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM