0

I wanted to make several files available to download for the users. I tried it like this but Django tries to open this url:

http://10.0.3.186:8000/var/www/openvpn/examples/easy-rsa/2.0/keys/first_key.key

I changed my template line like this:

<td width="100%" align = "right">
 <a href="http://10.0.3.186:8000/sslcert/{{ file }}/key_download">
  <font color = "white">&nbsp&nbspSSL-Key&nbsp&nbsp</font>
 </a>
</td>

I added following line to my urls

url(r'^(?P<username>\w+)/key_download$', views.key_download, name='key_download')

My views look like this

from django.utils.encoding import smart_str

def key_download(request, username):
    username_key = username + ".key"
    response = HttpResponse(mimetype='application/force-download')
    response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(username_key)
    response['X-Sendfile'] = smart_str("/var/www/openvpn/examples/easy-rsa/2.0/keys")
    return response

The file is getting downloaded and the filename is the right one, BUT it doesn't show any content at the moment.

BoJack Horseman
  • 4,406
  • 13
  • 38
  • 70
  • 1
    Could you add more details about what you're trying to do please? Are you trying to make an url that makes one particular file available, are you trying to let users get any file from a directory? – Tim Wilder Nov 28 '13 at 08:17
  • Well I am trying to make some SSL-Keys available which can be found in this directory: /var/www/openvpn/examples/easy-rsa/2.0/keys/ the file name depends on the username. This is the reason I used the {{ file }} tag plus the .key ending :) If {{ file }} would have the impact "userOne" I would make the key "userOne.key" available for download – BoJack Horseman Nov 28 '13 at 08:23

3 Answers3

1

You also need to set the HTTP header of the response to something like application/force-download. Please see docs.

See also this question.

Community
  • 1
  • 1
0xc0de
  • 8,028
  • 5
  • 49
  • 75
0

/var/www is not a URL. It's a file path on your server. If you want users to access a file in that directory, you have to configure Apache (or whatever) to serve it at a particular address, and then use that URL in your template rather than the file path.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • So how are you serving your site? You need to configure whatever server is doing that to also serve your file. – Daniel Roseman Nov 28 '13 at 09:38
  • From the code, it looks like- which file to serve, depends on some criteria (as it's not hardcoded, and sent from the controller). For example user specific files. You can't control serve of such files via apache or Nginx or any other server but your application. – 0xc0de Nov 28 '13 at 13:37
0

I solved the problem now.

def key_download(request, username):
    username_key = username +".key"
    fsock = open('/var/www/openvpn/examples/easy-rsa/2.0/keys/'+username_key, 'r')
    response = HttpResponse(fsock, mimetype='application/pgp-keys')
    response['Content-Disposition'] = "attachment; filename = %s " % (username_key)
    return response

A cool tip for the guys who might want to find out the mimetype

>>> import urllib, mimetypes
>>> url = urllib.pathname2url(filename)
>>> print mimetypes.guess_type(url)
BoJack Horseman
  • 4,406
  • 13
  • 38
  • 70