10

Im going over the django documentation and I found this piece of code that allows you to render a file as attachment

dl = loader.get_template('files/foo.zip')
context = RequestContext(request)
response = HttpResponse(dl.render(context), content_type = 'application/force-download')
response['Content-Disposition'] = 'attachment; filename="%s"' % 'foo.zip'
return response

The foo.zip file was created using pythons zipfile.ZipFile().writestr method

zip = zipfile.ZipFile('foo.zip', 'a', zipfile.ZIP_DEFLATED)
zipinfo = zipfile.ZipInfo('helloworld.txt', date_time=time.localtime(time.time()))
zipinfo.create_system = 1
zip.writestr(zipinfo, StringIO.StringIO('helloworld').getvalue())
zip.close()

But when I tried the code above to render the file, Im getting this error

'utf8' codec can't decode byte 0x89 in position 10: invalid start byte

Any suggestions on how to do this right?

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
helloworld2013
  • 3,094
  • 5
  • 19
  • 26
  • Why are you rendering a template which is a .zip file? Are you sure that's what's in the doc for serving files? – Paulo Bu Oct 18 '13 at 21:30
  • hello, I am trying to render it as an attachment. As far as I know you can specify the content_type of what you want to spit out in django reponse similar to the codes above – helloworld2013 Oct 18 '13 at 21:36
  • Are you sure _render_ is the proper word of what you want? I rather think you just want to _serve_ it for people to download it am I right? – Paulo Bu Oct 18 '13 at 21:37
  • If that's how you define it, okay. Bottom line is to make the file downloadable in django. – helloworld2013 Oct 18 '13 at 21:41
  • 2
    http://stackoverflow.com/questions/1156246/having-django-serve-downloadable-files – ScotchAndSoda Oct 18 '13 at 22:05

2 Answers2

19

I think what you want is to serve a file for people to download it. If that's so, you don't need to render the file, it's not a template, you just need to serve it as attachment using Django's HttpResponse:

zip_file = open(path_to_file, 'r')
response = HttpResponse(zip_file, content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="%s"' % 'foo.zip'
return response
Dharman
  • 30,962
  • 25
  • 85
  • 135
Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
  • Just a question about this solution, does python's open() automatically close after scope? With this you cannot close the stream until you return it to make it work. Thanks. – helloworld2013 Oct 18 '13 at 23:00
  • http://stackoverflow.com/questions/2404430/does-filehandle-get-closed-automatically-in-python-after-it-goes-out-of-scope – helloworld2013 Oct 18 '13 at 23:11
  • 1
    @helloworld2013: Sorry man I wasn't online. I guess you already answered your question :). (In a nutshell) when the zip_file gets out of scope and garbage collection do its thing, the resources for the file will be released. – Paulo Bu Oct 19 '13 at 00:09
13

FileResponse is preferred over HttpResponse for binary files. Also, opening the file in 'rb' mode prevents UnicodeDecodeError.

zip_file = open(path_to_file, 'rb')
return FileResponse(zip_file)
xtranophilist
  • 582
  • 8
  • 14
  • 1
    This worked beautifully for me. See https://docs.djangoproject.com/en/3.0/ref/request-response/#fileresponse-objects for more parameters such as specifying a target file name. – Scratcha Jun 05 '20 at 20:05