【发布时间】:2017-09-11 17:04:58
【问题描述】:
我正在尝试使用 falcon 框架作为后端来接收 pdf 文件。 我是后端的初学者,并试图了解正在发生的事情。所以总结一下,有2个类。其中之一是我正在工作的朋友。
这是后端代码:
#this is my code
class VehiclePolicyResource(object):
def on_post(self, req, resp, reg):
local_path = create_local_path(req.url, req.content_type)
with open(local_path, 'wb') as temp_file:
body = req.stream.read()
temp_file.write(body)
#this is my friend code
class VehicleOdometerResource(object):
def on_post(self, req, resp, reg):
local_path = create_local_path(req.url, req.content_type)
with open(local_path, 'wb') as temp_file:
body = req.stream.read()
temp_file.write(body)
完全一样,没有给出相同的答案,我通过这样做添加了路线
api.add_route('/v1/files/{reg}/policies',VehicleResourcesV1.VehiclePolicyResource())
并在终端中使用此命令:
HTTP POST localhost:5000/v1/files/SJQ52883Y/policies@/Users/alfreddatui/Autoarmour/aa-atlas/static/asd.pdf
它试图获取文件。但它一直说,不支持的媒体类型。
而其他代码,接收图像,与上面的代码相同,它可以工作。
有什么想法吗?
【问题讨论】:
-
抱歉命令错误:HTTP POST localhost:5000/v1/files/SJQ9957Y/policies @/Users/alfreddatui/Autoarmour/aa-atlas/static/asd.pdf
-
create_local_path中发生了什么?当传递的内容类型不符合要求时是否引发异常 -
哎呀抱歉,它创建了一个指向目录的字符串(我想将文件保存在该目录)这是代码:
def create_local_path(url, content_type): image_type = url.split('/')[-1] ext = mimetypes.guess_extension(content_type) name = '{}/{}{}'.format(image_type, uuid.uuid4(), ext) return os.path.join('./temp/', name) -
我觉得很好。
-
是的,我也是,看起来不错,但不起作用:(
标签: python backend falconframework