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

  • jkimisyellow...ish

    jkimisyellow

    8 Temmuz 2009
  • Lancome USA

    Lancome USA

    30 HAZİRAN 2009
  • TurkishRoyal

    TurkishRoyal

    16 Ocak 2007