简体   繁体   English

使用 flask 上传 json 文件时遇到问题

[英]Trouble uploading json file with flask

For some reason, this won't accept json files.出于某种原因,这不会接受 json 文件。

@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"> .您似乎已通过<input type="file" accept="json">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. (没有提供模板,所以我无法确定该行。这不是后端(烧瓶)的错误,而是您的模板代码(jinja/html)的错误。如果您能为此类提供MRE ,那就太好了问题。

For more information about <input type="file"> take a look at the MDN Documentation .有关<input type="file">的更多信息,请查看MDN 文档

Example of correct accept :正确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.这将只允许*.json文件,但请记住,用户可以手动提供其他文件并在解析/保存文件时创建后备或验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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