SORU
25 Mayıs 2009, PAZARTESİ


Üreten Django ile indirmek için dosya

Bir zıp arşivi ve indirmek için sunuyor, ama yine de sabit diskine bir dosya kaydetmek mümkün mü?

CEVAP
26 Mayıs 2009, Salı


Content-Disposition başlık ayarlamanız gerekir bir yükleme tetiklemek için:

from django.http import HttpResponse
from django.core.servers.basehttp import FileWrapper

# generate the file
response = HttpResponse(FileWrapper(myfile.getvalue()), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=myfile.zip'
return response

Eğer diskteki dosya istemesen bile StringIO kullanmak gerekir

import cStringIO as StringIO

myfile = StringIO.StringIO()
while not_finished:
    # generate chunk
    myfile.write(chunk)

Content-Length başlık olarak ayarlayabilirsiniz isteğe bağlı olarak:

response['Content-Length'] = myfile.tell()

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • David Wills

    David Wills

    31 Aralık 2007
  • My Name Is Jeff

    My Name Is J

    26 ŞUBAT 2008
  • Tech4Geeks

    Tech4Geeks

    8 Ocak 2012