Django download file cannot be opened properly

< H2 > problem phenomenon < / H2 >

download the file, which cannot be opened normally, as shown in the following figure

clipboard.png



,""

clipboard.png

< H2 > question sorting: (this download function uses StreamHttpResponse) < / H2 >

1. The downloaded file name has been specified in the source code:

response["Content-Disposition"] = "attachment;filename="{f_name}"".format(f_name=the_file_name)

but as a result, the downloaded files are all named "downloaded"

2, download function and data content are normal. should be an error in naming, and the file contains Chinese characters. Please correct it.

add:
python version 3.6
django version 1.11.13

< H2 > complete source code < / H2 >
from django.http import StreamingHttpResponse
-sharp 
def file_iterator(filename,chunk_size=512):
    with open(filename,"rb") as f:
        while True:
            c=f.read(chunk_size)
            if c:
                yield c
            else:
                break

-sharp 
def download_file(request):
    the_file_name = models.FileObj.objects.get(id=request.GET.get("id")).fileName  -sharp 
    file_path = os.path.join(file_dir,the_file_name) -sharp 
    response = StreamingHttpResponse(file_iterator(file_path))
    response["Content-Type"] = "application/octet-stream"
    response["Content-Disposition"] = "attachment;filename="{f_name}"".format(f_name=the_file_name)
    return response
Mar.20,2021

Open the web page to check, as shown in the following figure

clipboard.png

Content-Disposition

:

1


2


:BUG1BUG2


zip:
83f9f40699b429a21dbee5af2487788f.gif?_ga=2.176967205.1804310972.1529333628-377372411.1529333628

Menu