首页 > 解决方案 > Flask Restplus 文件上传问题

问题描述

我一直在寻找解决这个问题的方法,但运气不佳。我需要能够将图像上传到 Flask RESTplus API,但每次尝试时都会收到 400 Bad Request 错误,其中包含以下代码段:

{
  "errors": {
    "file": "label images Missing required parameter in an uploaded file"
  },
  "message": "Input payload validation failed"
}

下面是我正在使用的代码。如果它有任何相关性,我将这个 Flask 程序托管在 docker 图像上。

import werkzeug
from flask import Flask, send_file
from flask_restplus import Resource, Api, namespace
import os

# Declare API
app = Flask(__name__)
api = Api(app)
app.config["DEBUG"] = True

UPLOAD_FOLDER = 'temp/data'

upload_parser = api.parser()
upload_parser.add_argument('file',
                           type=werkzeug.FileStorage,
                           location='files',
                           required=True,
                           help='label images',
                           action='append')

ns_ocr = api.namespace('label_ocr', description='Methods for label OCR.')


# Upload method
@api.route('/upload/')
class UploadImage(Resource):
    @api.expect(upload_parser)
    def post(self):
        args = upload_parser.parse_args()

        uploaded_file = args['file']

        filename = 'picture.jpg'
        uploaded_file.save(os.path.join(UPLOAD_FOLDER, filename))
        return {}, 201

标签: pythonflask

解决方案


推荐阅读