0

I am trying to develop a flask server which generates a presentation object based on json data and files from a HTTP Post request. I am able to generate the file locally with same code but when I try to send it as an http response is fails.

Here is the code snippet for sending it as an http response -

prs_file_io = BytesIO()
prs.save(prs_file_io)
resp = Response()
resp.status_code = 200
resp.set_data(prs_file_io.getvalue())
return resp

And here is the python script sending the request and trying to save the file -

r = requests.post('http://localhost:8181/create-ppt',
                  #data=open('tile_resp.json', 'rb'),
                  files={'1': open('./0NtNFb0F9ch15fDrgYoECEpctPkjvayD.png', 'rb'),
                         'tile_data': open('tile_resp.json', 'rb')})
print(r.content)

And finally I pipe the output from request script to a pptx file.

But this is not working any idea what mistake I am making here?

yashdosi
  • 1,186
  • 1
  • 18
  • 40
  • I'm not that familiar with pptx, but I suppose your response should have a specific mimetype/Content-Type? – VKolev Dec 13 '16 at 10:55
  • Does `r.content` is exact data as you expect to get? – Andersson Dec 13 '16 at 10:58
  • @VKolev - Shouldn't the content type be automatically set? – yashdosi Dec 13 '16 at 10:59
  • @yashdosi I don't think so. It will be strange that prs_file_io.getvalue() will return Response-object, so You would have to make sure that the content-type/mimetype is what you expect. Since your are saving a file ... I will look in the headers of your Response. What happens when you open the url of your flask-application in your browser? – VKolev Dec 13 '16 at 11:04
  • @Andersson - There is binary data in r.content but when i write it to a pptx file it does not open. – yashdosi Dec 13 '16 at 11:08
  • @yashdosi, Do you really want to put binaries in `.pptx` file? Does it make sense? I think you don't have to open `json` as binary data. Try to replace `open('tile_resp.json', 'rb')` with `open('tile_resp.json')` – Andersson Dec 13 '16 at 11:15
  • @Andersson - I tried from curl command line and it works. So looks like problem is not on the server side. – yashdosi Dec 13 '16 at 11:29

3 Answers3

1

How about the following:

response = make_response(prs_file_io.get_value())
response.headers['Content-Type'] = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
response.headers['Content-Description'] = 'attachment; filename=example.pptx'
return response

make_response is a method from Flask see make_response()

Would that work if the response should be a pptx file?

VKolev
  • 855
  • 11
  • 25
0

I do this using

send_file

By doing:

from flask import send_file
from app import application
from pptx import Presentation
import os

prs=Presentation()
filename = os.path.join(application.root_path, 'test.pptx')
prs.save(filename)
return send_file(filename_or_fp=filename)

In my code, the application is defined in a python file in the app folder, hence the line:

from app import application

You'll have to change this for your application if you want to go this route.

0

This is an old question but none of the above worked for me, so I want to share my solution:

prs = Presentation(input)
file = io.BytesIO()
prs.save(file)

response = HttpResponse(content_type='application/vnd.ms-powerpoint')
response['Content-Disposition'] = 'attachment; filename="sample.pptx"'
response.write(file.getvalue())
file.close()
return response
Wikisaqui
  • 1
  • 1