【问题标题】:Serving files with Django and lighttpd使用 Django 和 lighttpd 提供文件
【发布时间】:2010-02-06 05:12:58
【问题描述】:

我正在尝试创建一种使用 Django 提供可下载内容的简单方法。这个想法是登录用户应该能够通过 lighttpd 下载(相当大的)文件。

在 SO 上有几篇关于此的帖子,我还遇到了 blog post 一个简单的解决方案。

我在上述链接中创建了一个视图(并在 lighttpd 配置中添加了“allow-x-send-file”=>“enable”),它有点“工作”。当我使用 Firebug 检查标题时,我得到了正确的内容类型、文件长度和 200 OK,但没有下载文件。

然后我找到了解决方案here on SO, where additional headers are sent。现在提供了一个文件,但下载的文件是空的。标题仍然正确。

这是我的来源(删除了 auth_decorators 并且不处理不存在的文件):

import os
import mimetypes
import django.http

from django.conf import settings

def get_absolute_filename(filename='', safe=True):
    if not filename:
        return os.path.join(settings.FILE_DOWNLOAD_PATH, 'index')
    if safe and '..' in filename.split(os.path.sep):
        return get_absolute_filename(filename='')
    return os.path.join(settings.FILE_DOWNLOAD_PATH, filename)

def retrieve_file(request, filename=''):
    abs_filename = get_absolute_filename(filename)
    response = django.http.HttpResponse(mimetype='application/force-download')
    response['X-Sendfile'] = abs_filename
    response['Content-Disposition'] = 'attachment; filename=%s' % abs_filename
    response['Content-Type'] = mimetypes.guess_type(abs_filename)
    response['Content-Length'] = os.path.getsize(abs_filename)
    return response

【问题讨论】:

  • 顺便说一句,请使用python-magic 而不是mimetypes

标签: django lighttpd


【解决方案1】:

查看您的来源 - 您不发送文件,只发送标题。

【讨论】:

【解决方案2】:

1.5 之前的 lighttpd 版本使用 X-LIGHTTPD-send-file 标头。

【讨论】:

    猜你喜欢
    • 2020-01-29
    • 2010-10-18
    • 1970-01-01
    • 2017-10-09
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多