pythonhtmlapple-walletpkpass

How to download a pkpass file converted to base64 using HTML?


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?


Solution

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

    passfile_base64 = base64.b64encode(passfile).decode('ascii')