支持大文件下载和Django文件中文名字
#iterator
def readFile(file_name):
f= open(file_name,'rb')
while True:
c= f.read(1024)
if c:
yield c
else:
break;
#down file
def down_File(request):
try:
file_name ='cfdjfdf008好好tf.mp4'
response = StreamingHttpResponse(readFile('D:/'+file_name))
response['mimetype']='application/octet-stream'
response['Content-Type']='text/plain'
# response['Content-Disposition']='attachment; filename*={}'.format(escape_uri_path(file_name))
response['Content-Disposition'] = "attachment; filename*=utf-8''{}".format(escape_uri_path(file_name))
return response;
except:
return HttpResponse("failed!please try it again!")
本文介绍了一个使用Django实现的大文件下载方法,该方法能够支持中文文件名的正确显示。通过定义`readFile`函数来逐块读取文件,并利用`StreamingHttpResponse`进行流式传输。
254

被折叠的 条评论
为什么被折叠?



