• <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 提供可下载文件

        Having Django serve downloadable files(让 Django 提供可下载文件)

            • <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 提供可下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                  <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>
                            <tbody id='o6HFs'></tbody>
                          • <legend id='o6HFs'><style id='o6HFs'><dir id='o6HFs'><q id='o6HFs'></q></dir></style></legend>