0

From my server, I'm reading my pkpass file and converting it to base64. I want to make this file downloadable on my template. Here's what I'm doing.

Server/Python (Django)

passfile_bytes = passfile.read()
passfile_base64 = base64.b64encode(passfile) # This is sent to template

Template/HTML

<a href="data:application/vnd.apple.pkpass;base64,{{ passfile_base64 }}" download="file.pkpass">
    Download
</a>

I know I'm messing up the href here because the download fails. How do I actually make this ready for download when the link is clicked?

darkhorse
  • 8,192
  • 21
  • 72
  • 148
  • Looks like it should work. Is the data url actually valid? If the base64 is mangled somehow then decoding would fail which would cause the download to fail. (There are base64 decoders online.) – Ouroborus Jan 05 '23 at 21:30

1 Answers1

1

base64.b64encode returns a bytes object, you need to convert it to a string:

passfile_base64 = base64.b64encode(passfile).decode('ascii')
thirtydot
  • 224,678
  • 48
  • 389
  • 349