AttributeError: 'NoneType' object has no attribute 'filename'. Flask-Restplus doesn't seem to recognize uploaded file as Filestorage object
AttributeError: 'NoneType' object has no attribute 'filename'. Flask-Restplus doesn't seem to recognize uploaded file as Filestorage object
我正在尝试上传 2 个文件(音频和图像文件)以及一些数据。我对使用 Flask 很陌生,但在查看其他人的文件存储问题后,我不确定我做错了什么。
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()
当我尝试发送数据时,我从发出请求的客户端收到 'Internal Server Error' 500。 flask-rest 服务器将抛出---
File "/home/joe/Projects/PyKapi-venv/kapi/resources/figure_resource.py", line 53, in post
image.filename,
AttributeError: 'NoneType' object has no attribute 'filename'
127.0.0.1 - - [20/May/2018 17:12:25] "POST /figures HTTP/1.1" 500 -
我认为问题出在我的 Http 请求中,但现在不太确定。我最初使用 Postman 发送请求,但最近改用 curl。这是我的 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
[更新] 我在 requestparser() 中错误地使用了 locations 参数。
location 参数需要请求的内容类型。于是我改成如下~
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()
`
我正在尝试上传 2 个文件(音频和图像文件)以及一些数据。我对使用 Flask 很陌生,但在查看其他人的文件存储问题后,我不确定我做错了什么。
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()
当我尝试发送数据时,我从发出请求的客户端收到 'Internal Server Error' 500。 flask-rest 服务器将抛出---
File "/home/joe/Projects/PyKapi-venv/kapi/resources/figure_resource.py", line 53, in post image.filename, AttributeError: 'NoneType' object has no attribute 'filename' 127.0.0.1 - - [20/May/2018 17:12:25] "POST /figures HTTP/1.1" 500 -
我认为问题出在我的 Http 请求中,但现在不太确定。我最初使用 Postman 发送请求,但最近改用 curl。这是我的 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
[更新] 我在 requestparser() 中错误地使用了 locations 参数。 location 参数需要请求的内容类型。于是我改成如下~
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()
`