我正在尝试上传2个文件(音频和图像文件)以及一些数据。我对使用Flask非常陌生,但在审查了Filestorage问题的其他人后,我不确定我做错了什么。
class FiguresResource(Resource):
parser = reqparse.RequestParser()
parser.add_argument(
'thing',
type=str)
parser.add_argument(
'image_file',
type=werkzeug.datastructures.FileStorage,
location=UPLOAD_FOLDER)
parser.add_argument(
'audio_file',
type=werkzeug.datastructures.FileStorage,
location=UPLOAD_FOLDER)
def post(self):
db = connect(MONGODB_DB, host=MONGODB_HOST, port=MONGODB_PORT)
data = self.parser.parse_args()
image = data['image_file']
audio = data['audio_file']
fig = Figure(
data['thing'],
image.filename,
get_file_size(image),
audio.filename,
get_file_size(audio)
)
image.save(image.filename)
audio.save(audio.filename)
fig.save()
db.close()
当我尝试发送数据时,我从请求客户端收到“内部服务器错误”500。烧瓶休息服务器将抛出---
文件“/home/joe/Projects/PyKapi-venv/kapi/resources/figure_resource.py”,第53行,在帖子中 image.filename, AttributeError:'NoneType'对象没有属性'filename' 127.0.0.1 - - [20 / May / 2018 17:12:25]“POST / figure HTTP / 1.1”500 -
我认为问题出在我的Http请求中,但现在不太确定。我最初是向Postman发送我的请求,但最近才转向使用curl。这是我的卷曲命令---
curl -F thing=Fruits -F image_file=@/home/joe/Projects/Pics/Fruits.jpg -F audio_file=@/home/joe/Projects/Audio/Fruits http://127.0.0.1:5000/figures
答案 0 :(得分:0)
[更新]我使用requestparser()中的locations参数错误。 位置争论需要请求的内容类型。所以我把它改成了如下〜
class FiguresResource(Resource):
parser = reqparse.RequestParser()
parser.add_argument(
'thing',
type=str,
location='form')
parser.add_argument(
'image_file',
type=FileStorage,
location='files')
parser.add_argument(
'audio_file',
type=FileStorage,
location='files')
def post(self):
data = self.parser.parse_args()
image = data['image_file']
audio = data['audio_file']
image_path = join(IMAGE_FOLDER, image.filename)
audio_path = join(AUDIO_FOLDER, audio.filename)
db = connect(MONGODB_DB, host=MONGODB_HOST, port=MONGODB_PORT)
if Figure.objects(visual_aid_path=image_path):
db.close()
return {"message": "Visual Aid file with that name already exists"}
if Figure.objects(audio_aid_path=audio_path):
db.close()
return {"message": "Audio file with that name already exists"}
fig = Figure(
data['thing'],
image_path,
audio_path
)
image.save(image_path)
audio.save(audio_path)
image.close()
audio.close()
fig.save()
db.close()
`