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

  • erikbjgn's channel

    erikbjgn's c

    12 Mayıs 2008
  • Mark Halberstadt

    Mark Halbers

    19 ŞUBAT 2010
  • Wronchi Animation

    Wronchi Anim

    9 Mayıs 2011