download the file, which cannot be opened normally, as shown in the following figure
,""
< 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
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