"""class GenerateParamsSchema(Schema):\n main_text = fields.Str(description='主题文字')\n high_light_text = fields.Str(description='强调文字')\n sub_text = fields.Str(description='副标题')\n content = fields.Str(description='描述文字')\n start_time = fields.Str(description='开始时间')\n end_time = fields.Str(description='结束时间')\n size = fields.Str(description='图片宽高')\n # size_type = EnumChoice(ImageSizeType).int_fields(allow_none=True) # 图片类型 1 像素 2 cm\n color_back = fields.Str(description='背景色')\n color_char = fields.Str(description='文字颜色')\n color_dot = fields.Str(description='点缀颜色')\n color_button = fields.Str(description='按钮颜色')\n CTA_button_text = fields.Str(description='按钮文字')\n CTA_button_border = fields.Int(description=CTABorderButton.state()) # 边框按钮\n CTA_button_horn = fields.Int(description=CTABorderButton.state()) # 直角矩形\n style_customize = fields.Int(description=StyleCustomizeType.state()) # CTA边框类型\n template = fields.Int(description=TemplateType.state()) # 模板类型\n pictures = fields.List(\n Upload(\n description='待上传图片',\n required=True,\n error_messages={'required': '待上传图片不能为空'}\n ),\n description='待上传图片列表',\n required=True,\n error_messages={'required': '待上传图片列表不能为空'}\n )\n\n\n\n@blp.route("/image")\n# class UploadView(JwtView):\nclass GenerateAPIView(MethodView):\n @blp.arguments(GenerateParamsSchema, location="form")\n @blp.arguments(Upload(required=True), location="files")\n @blp.response(code=200)\n def post(self, form_args, file_args):\n """生成图片"""\n print("form_args=", form_args)\n print("file_args=", file_args)\n return "hello"\n\n\n\n\n"""如果前端传参,同时需要传入数据和上传文件,怎么修改内容:如果前端需要同时传递数据和上传文件,可以使用multipart/form-data格式进行请求。修改代码如下:\n\npython\n@blp.route("/image")\nclass GenerateAPIView(MethodView):\n @blp.arguments(GenerateParamsSchema, location="form")\n @blp.arguments(Upload(required=True), location="files")\n @blp.response(code=200)\n def post(self, form_args, file_args):\n \"\"\"生成图片\"\"\"\n print("form_args=", form_args)\n print("file_args=", file_args)\n return "hello"\n\n\n在这个修改后的代码中,使用了@blp.arguments装饰器来分别指定了数据参数和文件参数的位置。数据参数使用location="form"表示在表单中,文件参数使用location="files"表示在文件字段中。然后在post方法的参数中分别接收了这两个参数,可以在方法中分别使用。\n"""\n

Python Flask BLP API: 上传图片和数据生成图片

原文地址: https://www.cveoy.top/t/topic/pZSG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录