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

  • EminemMusic

    EminemMusic

    9 ŞUBAT 2007
  • SuppressedStorm

    SuppressedSt

    11 AĞUSTOS 2013
  • Yanko Kral

    Yanko Kral

    8 HAZİRAN 2006