a survey about how to make a file dynamically and offer it to download,but still not save the file!

 To trigger a download you need to set Content-Disposition header:

from django.http importHttpResponse

from django.core.servers.basehttp importFileWrapper

 

# generate the file

response =HttpResponse(FileWrapper(myfile), content_type='application/zip')

response['Content-Disposition']='attachment; filename=myfile.zip'

return response

If you don't want the file on disk you need to use StringIO

 

import cStringIO asStringIO

 

myfile =StringIO.StringIO()

while not_finished:

    # generate chunk

    myfile.write(chunk)

Optionally you can set Content-Length header as well:

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

 

http://www.developer.com/tech/article.php/10923_3727616_2/Creating-Excel-Files-with-Python-and-Django.htm

http://djangosnippets.org/snippets/365/

http://djangosnippets.org/snippets/15/

this is a simple example to make a PDF file dynamically and offer to download!

https://docs.djangoproject.com/en/dev/howto/outputting-pdf/

 

posted on 2011-07-02 11:29  xuq  阅读(122)  评论(0编辑  收藏  举报

导航