• <bdo id='VLlyM'></bdo><ul id='VLlyM'></ul>

    <i id='VLlyM'><tr id='VLlyM'><dt id='VLlyM'><q id='VLlyM'><span id='VLlyM'><b id='VLlyM'><form id='VLlyM'><ins id='VLlyM'></ins><ul id='VLlyM'></ul><sub id='VLlyM'></sub></form><legend id='VLlyM'></legend><bdo id='VLlyM'><pre id='VLlyM'><center id='VLlyM'></center></pre></bdo></b><th id='VLlyM'></th></span></q></dt></tr></i><div id='VLlyM'><tfoot id='VLlyM'></tfoot><dl id='VLlyM'><fieldset id='VLlyM'></fieldset></dl></div>

        <small id='VLlyM'></small><noframes id='VLlyM'>

      1. <tfoot id='VLlyM'></tfoot><legend id='VLlyM'><style id='VLlyM'><dir id='VLlyM'><q id='VLlyM'></q></dir></style></legend>

        让 Django 提供可下载文件

        时间:2024-04-20

            • <bdo id='XyLEo'></bdo><ul id='XyLEo'></ul>
              • <i id='XyLEo'><tr id='XyLEo'><dt id='XyLEo'><q id='XyLEo'><span id='XyLEo'><b id='XyLEo'><form id='XyLEo'><ins id='XyLEo'></ins><ul id='XyLEo'></ul><sub id='XyLEo'></sub></form><legend id='XyLEo'></legend><bdo id='XyLEo'><pre id='XyLEo'><center id='XyLEo'></center></pre></bdo></b><th id='XyLEo'></th></span></q></dt></tr></i><div id='XyLEo'><tfoot id='XyLEo'></tfoot><dl id='XyLEo'><fieldset id='XyLEo'></fieldset></dl></div>

                <small id='XyLEo'></small><noframes id='XyLEo'>

                  <tbody id='XyLEo'></tbody>
                <legend id='XyLEo'><style id='XyLEo'><dir id='XyLEo'><q id='XyLEo'></q></dir></style></legend>
                <tfoot id='XyLEo'></tfoot>

                  本文介绍了让 Django 提供可下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  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 like this: http://example.com/download/?f=somefile.txt

                  And on the server, I know that all downloadable files reside in the folder /home/user/files/.

                  Is there a way to make Django serve that file for download as opposed to trying to find a URL and View to display it?

                  解决方案

                  For the "best of both worlds" you could combine S.Lott's solution with the xsendfile module: django generates the path to the file (or the file itself), but the actual file serving is handled by Apache/Lighttpd. Once you've set up mod_xsendfile, integrating with your view takes a few lines of code:

                  from django.utils.encoding import smart_str
                  
                  response = HttpResponse(mimetype='application/force-download') # mimetype is replaced by content_type for django 1.7
                  response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
                  response['X-Sendfile'] = smart_str(path_to_file)
                  # It's usually a good idea to set the 'Content-Length' header too.
                  # You can also set any other required headers: Cache-Control, etc.
                  return response
                  

                  Of course, this will only work if you have control over your server, or your hosting company has mod_xsendfile already set up.

                  EDIT:

                  mimetype is replaced by content_type for django 1.7

                  response = HttpResponse(content_type='application/force-download')  
                  

                  EDIT: For nginx check this, it uses X-Accel-Redirect instead of apache X-Sendfile header.

                  这篇关于让 Django 提供可下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Django中null=True和blank=True有什么区别? 下一篇:DRF:使用嵌套序列化程序的简单外键分配?

                  相关文章

                  <small id='o6HFs'></small><noframes id='o6HFs'>

                    <i id='o6HFs'><tr id='o6HFs'><dt id='o6HFs'><q id='o6HFs'><span id='o6HFs'><b id='o6HFs'><form id='o6HFs'><ins id='o6HFs'></ins><ul id='o6HFs'></ul><sub id='o6HFs'></sub></form><legend id='o6HFs'></legend><bdo id='o6HFs'><pre id='o6HFs'><center id='o6HFs'></center></pre></bdo></b><th id='o6HFs'></th></span></q></dt></tr></i><div id='o6HFs'><tfoot id='o6HFs'></tfoot><dl id='o6HFs'><fieldset id='o6HFs'></fieldset></dl></div>

                      <bdo id='o6HFs'></bdo><ul id='o6HFs'></ul>

                    1. <tfoot id='o6HFs'></tfoot>
                    2. <legend id='o6HFs'><style id='o6HFs'><dir id='o6HFs'><q id='o6HFs'></q></dir></style></legend>