How do i download and uploaded file in django database?

30 Views Asked by At

I have three different files in my database and they belong to users, like each user uploads three different files and they all have a "reg no" as a unique number. I want to download those files based on their "reg no". I will appreciate new code snippet.

# views.py

def download_file(request, regno):
    regno = request.session.get('reg_no_admin')
    file = CacAdmin.objects.get(regNo=regno)
    response = FileResponse(open(file.filePath, 'rb'))
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = f'attachment; filename="{file.fileName}"'
    return response
# urls.py

path('private/download-file/<int:regno>/', views.download_file, name="download_file"),

Template:

<a href="{% url 'download_file' file.regNo %}">Download File</a>
0

There are 0 best solutions below