Django下载文件 [英] Django download a file

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

问题描述

我对使用 Django 还很陌生,我正在尝试开发一个网站,用户可以在其中上传许多 excel 文件,然后将这些文件存储在媒体文件夹中 Webproject/project/media.

I'm quite new to using Django and I am trying to develop a website where the user is able to upload a number of excel files, these files are then stored in a media folder Webproject/project/media.

def upload(request):
    if request.POST:
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return render_to_response('project/upload_successful.html')
    else:
        form = FileForm()
    args = {}
    args.update(csrf(request))
    args['form'] = form

    return render_to_response('project/create.html', args)

然后该文档与他们上传的任何其他文档一起显示在列表中,您可以单击进入该列表,它将显示有关他们的基本信息以及他们上传的 excel 文件的名称.从这里我希望能够使用链接再次下载相同的 excel 文件:

The document is then displayed in a list along with any other document they have uploaded, which you can click into and it will displays basic info about them and the name of the excelfile they have uploaded. From here I want to be able to download the same excel file again using the link:

 <a  href="/project/download"> Download Document </a>

我的网址是

 urlpatterns = [

              url(r'^$', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25],
                                          template_name="project/project.html")),
              url(r'^(?P<pk>d+)$', DetailView.as_view(model=Post, template_name="project/post.html")),
              url(r'^upload/$', upload),
              url(r'^download/(?P<path>.*)$', serve, {'document root': settings.MEDIA_ROOT}),

          ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

但是我得到了错误,serve() 得到了一个意外的关键字参数文档根".谁能解释一下如何解决这个问题?

but I get the error, serve() got an unexpected keyword argument 'document root'. can anyone explain how to fix this?

解释我如何让上传的文件被选择并使用

Explain how I can get the uploaded files to to be selected and served using

def download(request):
    file_name = #get the filename of desired excel file
    path_to_file = #get the path of desired excel file
    response = HttpResponse(mimetype='application/force-download')
    response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
    response['X-Sendfile'] = smart_str(path_to_file)
    return response

推荐答案

您在参数文档_root 中遗漏了下划线.但是在生产中使用 serve 是个坏主意.改用这样的东西:

You missed underscore in argument document_root. But it's bad idea to use serve in production. Use something like this instead:

import os
from django.conf import settings
from django.http import HttpResponse, Http404

def download(request, path):
    file_path = os.path.join(settings.MEDIA_ROOT, path)
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
            response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
            return response
    raise Http404

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆