0

I try to generate a file, whose mime-type I don't know, to let the user download it via flask and it just creates a textfile.

Context: The user enters a Base64 encoded file, the site should decode it end let the user download this file.

Current state:
(decodedText is the decoded base64)

from flask import make_response 

@app.route('/tools/base64', methods=['GET', 'POST'])  
def base64():  
    response = make_response(decodedText)  
    response.headers["Content-Disposition"] = "attachment; filename=file"  
    response.headers["Content-Type"] = "application/octet-stream"  
    return response

The result of this is a textfile with content like

b'\xff\xd8\xff\xe0\x00\x10'

What I tried:
The "Content-Type"-Line is i.e. one line I found searching the internet, this should tell the browser that it's a binary - and firefox says now that it's a binary, but when downloaded it's still text... I also tried removing the b'', but I guess then it was explicit text.

So the question is: what do I need to tell python/flask so it's an actual binary file which will be downloaded?

Thank you very much in advance.

bale
  • 23
  • 4
  • Why do you think this is a text file? It doesn't look like it from what you've posted. – pvg Jan 11 '16 at 01:33
  • Because it can't be opened as the image it should be. Maybe it is no real text file as long as there are the b'' signs (b'' says binary), but if it's binary it's definitely broken. When I open it in hexedit instead of the ASCII-"Translation" on the right there is the b'\xff\xd8\xff\xe0\x00\x10' stuff. – bale Jan 12 '16 at 13:22
  • Then the problem is almost certainly with your data. See http://stackoverflow.com/questions/11017466/flask-return-image-created-from-database – pvg Jan 12 '16 at 15:53

1 Answers1

0

The file you have has some binary data in it. When you download it, it still has that same data. The thing is that files do not actually have an explicit type. File extensions are a convention we use to keep track of what is in a file and what programs can use the files appropriately. When you download your file you are likely downloading something with no extension. Your computer may be defaulting to treating it like a text file.

For example, if you have the binary for a .png image, you could download that into a file and open it with an image viewer. For convenience you could rename it something that ends with .png.

skyler
  • 1,487
  • 1
  • 10
  • 23
  • Hi and thanks for your answer, but I already tried that. I declared it as jpg (by adding .jpg to the name) but when I try to open I get an error message saying that something is wrong with this file. – bale Jan 12 '16 at 13:20