Reputation: 13763
I am trying to send a zip file to frontend so that it could download it in the browser.
Zip file has a folders inside and those folders have files:
file.zip
- first folder
- file1.pdf
- file2.pdf
- second folder
- file3.pdf
I think that I need to convert the file to bytes first to send it as a response so I tried to do this:
zip_file = ZipFile(zip_file_path)
zip_byte_array = bytearray()
for filename in zip_file.namelist():
byte_content = zip_file.read(filename)
zip_byte_array.append(byte_content)
return Response(zip_byte_array)
It gives the following error while appending to the bytearray:
an integer is required
the folder was archived like this:
zip_file_path = shutil.make_archive(dir_path, 'zip', dir_path)
How can I fix this?
Upvotes: 3
Views: 5075
Reputation: 13763
OK, it turns out it is a little easier than I thought. I could easily do this:
zip_file = open(zip_file_path, 'rb')
response = HttpResponse(zip_file, content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=name.zip'
return response
Upvotes: 8
Reputation: 16224
Appending a byte array is assumed to be executed with an integer that will have the value of the appended byte, since append
is mostly comprehended as an operation of adding an item to an array, and bytearray
is a numerical sequence.
For arrays concatenation, just use the +
operator, like strings:
zip_byte_array += byte_content
Upvotes: 0