Having Django serve downloadable files

后端 未结 15 1564
野的像风
野的像风 2020-11-22 05:46

I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.

For instance, I\'d like the URL to be something

相关标签:
15条回答
  • 2020-11-22 06:25

    Try: https://pypi.python.org/pypi/django-sendfile/

    "Abstraction to offload file uploads to web-server (e.g. Apache with mod_xsendfile) once Django has checked permissions etc."

    0 讨论(0)
  • 2020-11-22 06:27

    Django recommend that you use another server to serve static media (another server running on the same machine is fine.) They recommend the use of such servers as lighttp.

    This is very simple to set up. However. if 'somefile.txt' is generated on request (content is dynamic) then you may want django to serve it.

    Django Docs - Static Files

    0 讨论(0)
  • 2020-11-22 06:30

    You should use sendfile apis given by popular servers like apache or nginx in production. Many years i was using sendfile api of these servers for protecting files. Then created a simple middleware based django app for this purpose suitable for both development & production purpose.You can access the source code here.
    UPDATE: in new version python provider uses django FileResponse if available and also adds support for many server implementations from lighthttp, caddy to hiawatha

    Usage

    pip install django-fileprovider
    
    • add fileprovider app to INSTALLED_APPS settings,
    • add fileprovider.middleware.FileProviderMiddleware to MIDDLEWARE_CLASSES settings
    • set FILEPROVIDER_NAME settings to nginx or apache in production, by default it is python for development purpose.

    in your classbased or function views set response header X-File value to absolute path to the file. For example,

    def hello(request):  
       // code to check or protect the file from unauthorized access
       response = HttpResponse()  
       response['X-File'] = '/absolute/path/to/file'  
       return response  
    

    django-fileprovider impemented in a way that your code will need only minimum modification.

    Nginx configuration

    To protect file from direct access you can set the configuration as

     location /files/ {
      internal;
      root   /home/sideffect0/secret_files/;
     }
    

    Here nginx sets a location url /files/ only access internaly, if you are using above configuration you can set X-File as,

    response['X-File'] = '/files/filename.extension' 
    

    By doing this with nginx configuration, the file will be protected & also you can control the file from django views

    0 讨论(0)
  • 2020-11-22 06:31

    Providing protected access to static html folder using https://github.com/johnsensible/django-sendfile: https://gist.github.com/iutinvg/9907731

    0 讨论(0)
  • 2020-11-22 06:35

    A "download" is simply an HTTP header change.

    See http://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment for how to respond with a download.

    You only need one URL definition for "/download".

    The request's GET or POST dictionary will have the "f=somefile.txt" information.

    Your view function will simply merge the base path with the "f" value, open the file, create and return a response object. It should be less than 12 lines of code.

    0 讨论(0)
  • 2020-11-22 06:36
    def qrcodesave(request): 
        import urllib2;   
        url ="http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=s&chld=H|0"; 
        opener = urllib2.urlopen(url);  
        content_type = "application/octet-stream"
        response = HttpResponse(opener.read(), content_type=content_type)
        response["Content-Disposition"]= "attachment; filename=aktel.png"
        return response 
    
    0 讨论(0)
提交回复
热议问题