0

I'm using api_hour for API web server and trying to response to request with excel file. But it is much more difficult than I thought.

If I use nodejs or django, it is fine and there are many guide for it. But api_hour is not. The following code is mine.

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'])

What I found is that the other frameworks, for example nodejs, django, and ASP.NET Core, respond with file using there own encapsulated functions so do not assign binary data, which is response['excel'] in this code, directly to body.

Is there any way to respond with file ? especially excel ? thanks.

wallah
  • 1,964
  • 5
  • 27
  • 48

1 Answers1

1

It looks like Response is aiohttp.web.Response.

You may thus want to look into the StreamResponse class instead, which you can stream data into, if that's what you're asking.

Or, if you need to (try to) force the client to download your data as a file, add a Content-Disposition header:

Response(
    ...,
    headers={"Content-Disposition": "attachment; filename=my-excel.xlsx"},
)
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Yes, it is aiohttp.web. And I missed write I already write header before example code. Sorry about that. And I tried with your answer. But it doesn't work... – wallah Jan 07 '19 at 11:23