Downloaded files

views.py

from django.shortcuts import render, HttpResponse
from django.http import FileResponse
from downfile.models import Demo
import zipfile

addzip DEF (Request):
zip_file_name = 'Archive.zip' archive file name #
# The data read out of the database
for I in Range (. 1,. 3):
with Open (F '.py} {I', ' WB ') AS F1:
obj = Demo.objects.get (ID = I)
f1.write (obj.file_data)
# compressed file to the folder
F = zipfile.ZipFile (zip_file_name,' A ', zipfile.ZIP_DEFLATED)
F .write (F '{I} .py')
f.close ()
# send a file to download distal
file Open = (zip_file_name, 'RB')
Response = FileResponse (file)
Response [ 'the Type-the Content'] = ' file application / OCTET-Stream '
Response [' the Content-Disposition '] = f'attachment; filename = "{} zip_file_name"'

return response

Guess you like

Origin www.cnblogs.com/wanghuijie1/p/11803030.html