- 論壇徽章:
- 0
|
各位朋友們好:
我使用django的功能想實現(xiàn)下載一個大的**.zip包的功能,該包大小差不多1個G
網(wǎng)上查了一些方法:本來是準備用遍歷文件目錄逐個文件打包的,但是我需要打包的文件很多,用這個方法很慢,而且由于我里面有中文文件,遍歷文件路徑還會報錯。因此我選擇了,直接我先把所有文件打包到名叫test.zip中,希望用django實現(xiàn)直接下載test.zip包的功能,但是嘗試了好幾次,都報錯:而且保存的原因提示是內存使用過大這種,我看了網(wǎng)上的大文件下載,用的就是下面這個方法:
19.def send_zipfile(request):
20. """
21. Create a ZIP file on disk and transmit it in chunks of 8KB,
22. without loading the whole file into memory. A similar approach can
23. be used for large dynamic PDF files.
24. """
25. temp = tempfile.TemporaryFile()
26. archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
28. filename = “test.zip” # Select your files here.
29. archive.write(filename)
30. archive.close()
31. wrapper = FileWrapper(temp)
32. response = HttpResponse(wrapper, content_type='application/zip')
33. response['Content-Disposition'] = 'attachment; filename=test.zip'
34. response['Content-Length'] = temp.tell()
35. temp.seek(0)
36. return response
希望朋友們能指點一下我這段代碼哪里不對,或者有相關直接用django下載zip包的代碼也麻煩告知一下。
萬分感謝了。
|
|