0

我正在尝试上传 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()

当我尝试发送数据时,我从请求的客户端收到“内部服务器错误”500。烧瓶休息服务器将抛出——

文件“/home/joe/Projects/PyKapi-venv/kapi/resources/figure_resource.py”,第 53 行,在 post image.filename 中,AttributeError:'NoneType' 对象没有属性 '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
4

1 回答 1

0

[更新] 我在 requestparser() 中使用了位置参数错误。位置争论需要请求的内容类型。所以我改了如下~

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()

`

于 2018-05-22T00:15:57.553 回答