烧瓶读取上传的json文件 [英] Flask read uploaded json file
问题描述
我正在通过烧瓶上载json文件,但实际上无法读取文件中的内容.
I'm uploading a json file via flask, but I'm having trouble actually reading what is in the file.
# named fJson b/c of other json imports
from flask import json as fJson
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
file = request.files['file']
# data = fJson.load(file)
# myfile = file.read()
我正试图通过使用'file'变量来解决这个问题.我看了 http://flask.pocoo.org/docs/0.10/api/#flask.json.load ,但出现错误无法解码JSON对象".我还查看了读取文件数据而不将其保存在Flask中建议使用file.read(),但该方法无效,返回"None"或".
I'm trying to deal with this by using the 'file' variable. I looked at http://flask.pocoo.org/docs/0.10/api/#flask.json.load, but I get the error "No JSON object could be decoded". I also looked at Read file data without saving it in Flask which recommended using file.read(), but that didn't work, returns either "None" or "".
推荐答案
由于某种原因,文件中的位置位于末尾.做
For some reason the position in the file was at the end. Doing
file.seek(0)
在读取或加载之前解决了该问题.
before doing a read or load fixes the problem.
这篇关于烧瓶读取上传的json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!