django 文件下载

前端界面 templates

<div class="container">
        <div class="row">

            <div class="col-md-8 col-md-offset-2">
                <div><h3 style="text-align: center">下载模板</h3></div>
                <form method="post" enctype="multipart/form-data" action="{% url 'download' %}"
                      class="table table-bordered form-horizontal">
                    {% csrf_token %}
                    <input type="file" name="xsfile"/>
                    <input type="submit" value="提交"/>
                </form>

            </div>

        </div>
    </div>

urls.py

url(r'^download/',download,name='download'),
 
 

views.py

from django.http import JsonResponse, FileResponse

 
 

def download(request):
    '''
    download下载模板
    :param request:
    :return:
    '''
    the_file_name = 'down.xlsx'
    filename = 'download/down.xlsx'
    file = open(filename, 'rb')
    response = FileResponse(file)
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="{0}"'.format(the_file_name)
    return response

结果:


猜你喜欢

转载自blog.csdn.net/hejunw/article/details/80192666