I'm loading an archive from Github and want to return it to the browser to download. I successfully get the file and can save it to the local disc (I can open the archive and it's normal - size and content):
headers = {'Authorization': 'token ' + github_token}
zip_response = requests.get(github_url, headers = headers)
output = open("archive.zip", "wb")
output.write(zip_response.content)
output.close()
But when I return it to the client, it's bigger almost twice and archive is corrupted:
zip_content = io.BytesIO(zip_response.content)
return send_file(zip_content,
mimetype='application/zip',
attachment_filename = 'archive.zip')
Javascript:
var url = window.URL.createObjectURL(new Blob([response.data]), {type: 'application/zip'});
var link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'archive.zip');
document.body.appendChild(link);
link.click();
I was trying to use other solutions from this problem: Download File from Bytes in JavaScript
but nothing works.
This just returns 0 bytes:
var bytes = new Uint8Array(response.data)
This throws the exeption "Failed to execute 'atob' on 'Window': The string to be decoded contains characters outside of the Latin1 range.":
var binaryString = window.atob(response.data);
Upd: I found response.data has the normal length but when I create a new Blob object it's 2 times more, I don't know why.