Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

python - Download zip file with Django from API request

Currently, I am making a call to an API that sends back a zip file. When using request:

res = requests.get('http://url/to/api, headers={auth: 'token', Accept: 'application/octet-stream'} 

I am able to get the binary response content using res.content:

Example:

b'PKx05x06...

Then I return the binary response from my view to my template:

return render(request, 'example.html', {'zipfile': res.content}

At my example.html, I tried to create a button to download the zip file using:

<a href="data:application/zip, {{ zipfile }}" download>Download</a>

However, I got back a corrupted zip file.

I'm quite new to Django, any help here is appreciated, thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...