Django download file vuoto
Domanda
Sto scrivendo una semplice funzione per scaricare un certo file, dal server, alla mia macchina. Il file è unica rappresentata dal suo id. Il file viene locatd corectly, e il download è fatto, ma il file scaricato (anche se il nome di quello sul server) è vuota. Il mio download funzione è simile a questo:
def download_course(request, id):
course = Courses.objects.get(pk = id).course
path_to_file = 'root/cFolder'
filename = __file__ # Select your file here.
wrapper = FileWrapper(file(filename))
content_type = mimetypes.guess_type(filename)[0]
response = HttpResponse(wrapper, content_type = content_type)
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = 'attachment; filename=%s/' % smart_str(course)
return response
, dove posso essere sbagliato? grazie!
Soluzione
ho risposto a questa domanda qui , spero che aiuta.
Altri suggerimenti
Sembra che tu non stai inviando i dati (che non hanno nemmeno aprire il file).
Django ha una bella wrapper per l'invio di file (codice preso dal djangosnippets.org ):
def send_file(request):
"""
Send a file through Django without loading the whole file into
memory at once. The FileWrapper will turn the file object into an
iterator for chunks of 8KB.
"""
filename = __file__ # Select your file here.
wrapper = FileWrapper(file(filename))
response = HttpResponse(wrapper, content_type='text/plain')
response['Content-Length'] = os.path.getsize(filename)
return response
così si potrebbe usare qualcosa come response = HttpResponse(FileWrapper(file(path_to_file)), mimetype='application/force-download')
.
Se siete veramente utilizza lighttpd (a causa della X " sendfile " intestazione), si dovrebbe verificare il server e la configurazione FastCGI, immagino.
Prova uno di questi approcci:
1) Disattivare GZipMiddleware se lo si utilizza;
2) Applicare una patch per Django / core / server / basehttp.py descritti in https://code.djangoproject.com/ticket/6027