方法一、
from django.http import StreamingHttpResponse def big_file_download(request): # do something... def file_iterator(file_name, chunk_size=512): with open(file_name) as f: while True: c = f.read(chunk_size) if c: yield c else: break the_file_name = "file_name.txt" response = StreamingHttpResponse(file_iterator(the_file_name)) return response
方法二、
csv格式
#urls.py url(r'download/(?P<FILE>.*)$', views.DownLoadView.as_view(), name='download'), #views.py class DownLoadView(View): def get(self, request, FILE): file_path = STATICFILES_DIRS[0] +"/"+FILE response = HttpResponse(content_type="text/csv") response['Content-Disposition'] = 'attachment; filename=%s' %FILE response['Content-Length'] = os.path.getsize(file_path) data = csv.reader(file(file_path, 'rb')) writer = csv.writer(response) for item in data: writer.writerow(item) return response
方法三、
#urls.py url(r'loadkml/(.*)$', 'django.views.static.serve', {'document_root':settings.DOWNLOAD_DIR}, name='loadkml'), #settings.py DOWNLOAD_DIR = os.path.join(BASE_DIR, "data").replace('\', '/')