【问题标题】:download file via url python通过url python下载文件
【发布时间】:2016-03-03 11:02:53
【问题描述】:

我正在尝试通过 url 下载文件。它通常是下载的,但问题是,当我试图打开文件时,它有 0 个字节。

有没有人遇到过这个问题,或者有想法从哪里来?

这是我的代码

def download():
    file_name = "/opt/static/avatar/20/mouse.png"
    response = HttpResponse(content_type='application/force-download')
    response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
    response['X-Sendfile'] = smart_str(path)
    return response 

【问题讨论】:

  • 请注明您使用的web框架?
  • 您是否尝试将content_type 设置为application/binary
  • @Selcuk 框架是 Django
  • X-sendfile 是一个非标准的标头。您的具体配置是什么(应用服务器、Web 服务器、Django 版本)?
  • 你的网络服务器是什么? nginx、apache 还是别的什么?

标签: python django file download


【解决方案1】:

NginX 不支持X-Sendfile 标头。您必须改用X-Accel-Redirect

response['X-Accel-Redirect'] = smart_str(path)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 2015-03-28
    • 2012-08-06
    • 1970-01-01
    • 2015-06-13
    • 2012-10-04
    • 2016-09-04
    • 1970-01-01
    相关资源
    最近更新 更多