A
A
alxxla2021-11-24 14:07:56
Django
alxxla, 2021-11-24 14:07:56

How to organize the download of zip'a, which I form "on the fly"?

How to organize the download of zip'a, which I form "on the fly"?

def get_contract_and_payment(contract_number):
    contract = Contract.objects.get(number=contract_number)
    signed_contract = contract.signed_contract.path
    payment = contract.payment.path
    contract_and_payment = [signed_contract, payment]

    return contract_and_payment


def download(contract_number):
    files = get_contract_and_payment(contract_number)
    zip_subdir = str(contract_number)
    zip_filename = f'{zip_subdir}.zip'

    b = BytesIO()
    zip_file = zipfile.ZipFile(b, 'w')

    for file_path in files:
        file_dir, file_name = os.path.split(file_path)
        zip_path = os.path.join(zip_subdir, file_name)

    zip_file.close()
    b.seek(0)

    response = HttpResponse(b.getvalue(), content_type="application/x-zip-compressed")
    response['Content-Disposition'] = 'attachment;filename=' + zip_filename

    return response

How to bring the download to mind?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question