Python 响应 excel 文件
Python response with excel file
我正在为 API 网络服务器使用 api_hour 并尝试使用 excel 文件响应请求。但这比我想象的要困难得多。
如果我使用 nodejs 或 django,那很好,而且有很多指南。但是 api_hour 不是。以下代码是我的。
headers = {
'Content-Disposition': 'attachment; filename="excel_file.xlsx"',
'Access-Control-Allow-Headers': 'status,Origin, X-Requested-With, Content-Type, Cookie, Accept, X-PINGOTHER',
'Access-Control-Allow-Methods': '*',
'Accept-Ranges': 'bytes'
}
self.responseHeaders = multidict.MultiDict(headers, )
return Response(content_type='application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet',
status=self.status,
headers=self.responseHeaders,
body=response['excel'])
我发现的是其他框架,例如nodejs、django、ASP.NET Core,用文件响应有自己的封装函数所以不要将二进制数据,在这段代码中是response['excel']
,直接分配给body。
有什么方法可以回复文件吗?特别是 excel ?谢谢。
它looks like Response
is aiohttp.web.Response
.
因此,您可能想查看 StreamResponse
class,如果您要求的话,您可以将数据流式传输到其中。
或者,如果您需要(尝试)强制客户端将您的数据下载为文件,请添加 Content-Disposition
header:
Response(
...,
headers={"Content-Disposition": "attachment; filename=my-excel.xlsx"},
)
我正在为 API 网络服务器使用 api_hour 并尝试使用 excel 文件响应请求。但这比我想象的要困难得多。
如果我使用 nodejs 或 django,那很好,而且有很多指南。但是 api_hour 不是。以下代码是我的。
headers = {
'Content-Disposition': 'attachment; filename="excel_file.xlsx"',
'Access-Control-Allow-Headers': 'status,Origin, X-Requested-With, Content-Type, Cookie, Accept, X-PINGOTHER',
'Access-Control-Allow-Methods': '*',
'Accept-Ranges': 'bytes'
}
self.responseHeaders = multidict.MultiDict(headers, )
return Response(content_type='application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet',
status=self.status,
headers=self.responseHeaders,
body=response['excel'])
我发现的是其他框架,例如nodejs、django、ASP.NET Core,用文件响应有自己的封装函数所以不要将二进制数据,在这段代码中是response['excel']
,直接分配给body。
有什么方法可以回复文件吗?特别是 excel ?谢谢。
它looks like Response
is aiohttp.web.Response
.
因此,您可能想查看 StreamResponse
class,如果您要求的话,您可以将数据流式传输到其中。
或者,如果您需要(尝试)强制客户端将您的数据下载为文件,请添加 Content-Disposition
header:
Response(
...,
headers={"Content-Disposition": "attachment; filename=my-excel.xlsx"},
)