Coke Machine
Coke Machine

Reputation: 25

Setting up flask.response in python

I was wondering if you are allowed to have both a content-type and content disposition within one header when you are sending a flask response. Currently, my code is running as:

headers = {
  "Content-Type" : mimetype,
  "Content-Disposition":
  "attachment;filename={}".format(flask.request.args.get('filename', key)),
}
return flask.Response(data, 200, headers)

but when I click on the link to download it, it comes back as a text document with the name "download". When I change the file type to what it is supposed to be it becomes the correct file likewise when I right click save as with the file link.

Is there a problem with setting my content-type and content-disposition in the same headers? Because I don't think the content-disposition is being registered as the file wouldn't be coming back as a text document with the name download.

Upvotes: 1

Views: 741

Answers (1)

Prahlad Yeri
Prahlad Yeri

Reputation: 3653

I'm using a slightly different method of creating the response that works:

from flask import make_response
##
##
resp = make_response(logs)
filename = "applog_%s.log" % datetime.now()
filename = filename.replace(" ", "_")
resp.headers['Content-Disposition'] = 'attachment;filename=' + filename
resp.mimetype = "text/plain"
return resp

Edit

And also make sure there aren't any spaces in your filename argument. I remember that gave me hard time once, that's why I replaced the spaces with underscores as you can see in the above code.

Upvotes: 2

Related Questions