python - 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
解决方案
推荐阅读
- java - 预览速度模板
- php - PHP POST 大摇大摆
- android - 如何从匿名路径的实时数据库(Firebase)中检索数据
- ios - 带有嵌入式 Citrix MDX 工具包的 iOS 应用程序因“无法获取本地化包”错误而崩溃
- java - 关于 log4j 属性和 maxfilesize
- cakephp - Cakephp + Foundation (sass)
- javascript - 用打字稿比较两个十进制值
- python - 高斯过程作为 CNN 的最后一层
- javascript - Internet Explorer Javascript 语法错误
- ios - 音频无法正常播放