Django 之 下载文件

法I:

views.py

 1 #encoding:utf-8
 2 import os
 3
 4 from django.core.servers.basehttp import FileWrapper
 5 from django.http import HttpResponse
 6
 7 path = ‘/tmp/‘
 8 def downloader(request):
 9     filename_tmp = ‘test.tmp‘   # test.tmp为将要被下载的文件名
10     filename = os.path.join(path,filename_tmp)
11     wrapper = FileWrapper(file(filename))
12     response = HttpResponse(wrapper, content_type=‘text/plain‘)
13     response[‘Content-Length‘] = os.path.getsize(filename)
14     response[‘Content-Disposition‘] = ‘attachment; filename="somefilename.csv"‘    # somefilename.csv为下载后的文件名
15     return response

法II:

test.html

<a href="download/file/">下载</a>

urls.py

1 url(r‘^download/file/$‘, ‘xxx.views.download‘),    # xxx为项目名

xxx中的views.py

 1 import os
 2 from django.http import HttpResponse
 3 from django.contrib.auth.decorators import login_required
 4
 5 @login_required
 6 def download(request):
 7     response = HttpResponse()
 8     response[‘Content-Disposition‘] = ‘attachment;filename=downfile.txt‘    # downfile.txt为下载后的文件名
 9     full_path = os.path.join(‘/tmp‘, ‘filename.txt‘)    # filename.txt为将要被下载的文件名
10     if os.path.exists(full_path):
11         response[‘Content-Length‘] = os.path.getsize(full_path)    #  可不加
12         content = open(full_path, ‘rb‘).read()
13         response.write(content)
14         return response
15     else:
16         return HttpResponse(u‘文件未找到‘)

法III:

test.html

<a href="download/downfile.txt">下载</a>

urls.py

 1 url(r‘^download/(?P.*)$‘, ‘django.views.static.serve‘,{‘document_root‘:文件路径}), 
时间: 2024-08-16 03:30:48

Django 之 下载文件的相关文章

django 中下载文件与下载保存为excel

一.django 中下载文件 在实际的项目中很多时候需要用到下载功能,如导excel.pdf或者文件下载,当然你可以使用web服务自己搭建可以用于下载的资源服务器,如nginx,这里我们主要介绍django中的文件下载. 1.前端 实现方式:a标签+响应头信息(当然你可以选择form实现) <div class="col-md-4"><a href="{% url 'download' %}" rel="external nofollow

Django动态下载文件

前台提交查询条件,下载符合条件的EXCEL数据文件,后端视图中使用 xlwt 库来返回,如: objs = Units.objects.all() # 创建 Workbook 时,如果需要写入中文,请使用 utf-8 编码,默认是 unicode 编码. wb = xlwt.Workbook(encoding='utf-8') ws = wb.add_sheet('配件价格') ws.write(0, 0, '配件编号') ws.write(0, 1, '配件名称') ws.write(0, 2

Django下载文件

服务器端: f = open(filename) data = f.read() f.close() response = HttpResponse(data,mimetype='application/octet-stream') response['Content-Disposition'] = 'attachment; filename=%s' % filename return response 用户端: #coding=utf-8 import urllib,time,datetime

Django压缩包下载

前言 系统有的时候需要下载一些内容到本地,这些内容原来可能就是在服务器上某个位置或者离散分布,文件内容格式多样而且大小不一,本文将一步步来解决这些问题. 本文环境: Python 2.7.10 Django 1.11 zip-file 安装 执行pip安装 pip install zipfile python自带,不需要安装 打包完整目录 下面代码将完全打包当前目录,保存为abcd.zip文件里,存放到当前目录 存放在当前目录有个问题:压缩包里会再次包含压缩包,大小为0.所以可以将压缩包放到另外

使用StreamHttpResponse和FileResponse下载文件的注意事项及文件私有化

为什么需要编写下载视图方法? 你或许知道,我们上传的文件默认放在media文件夹中的,且Django会为每个上传的静态文件分配一个静态url.在模板中,你可以使用{{ mymodel.file.url }}获取每个文件的链接(url),浏览器也是可以直接打开这个url的,如下所示. <td><a href="/media/files/b1957d79f3.JPG/">/media/files/b1957d79f3.JPG</a></td>

django的下载安装与简单应用

Django下载安装与简单实例 Django官网下载页面 1.下载Django: pycharm中下载 pycham中装模块的地方安装 或界面下Terminal安装File | Settings | Project: tushu | Project Interpreter pip3下载 pip3 install django==1.11.9 -i http://xxxxxx 指定源 2.创建一个django project 创建项目 django-admin startproject mysit

wp8通过WebClient从服务器下载文件

通过WebClient从Web服务器下载文件,并保存到wp8手机应用程序的独立存储. 我们可以通过利用webClient_DownloadStringCompleted来获得下载完成所需要的时间,用Stopwatch得到下载的总时间. 通常我们都将上传.下载作为异步事件来处理,以便不阻止主线程. String url = "http://172.18.144.248:8080/upload/" + filename; WebClient client = new WebClient()

阿里云服务器(Windows)如何下载文件

背景:公司只有我一个技术,在我之前还有一个老技术,属于兼职状态,为了尽快熟悉公司网站及app项目情况,我联系了老技术,请他尽快将代码发给我,他说代码文件过大,问我能不能连上服务器下载.百度了很多,都不得要领,便有了这篇文的因由.当然,后来发现是自己百度的方向不对,这是后话暂且不提. 1.登录阿里云官网(https://www.aliyun.com/),[控制台]点进去 2.最左边列表点击[云服务器ECS] 3.[运行中]点进去 4.获取公网IP地址 5.[开始]菜单输入mstsc,打开本机的"远

TreadAPP-使用线程下载文件

package main; /** * Created by lxj-pc on 2017/6/27. */public class TreadApp {//volatile 线程间共享变量 private static volatile boolean isExit=false;//static成员不能访问非static成员 public static void main(String[] args) { //下载一个文件 启动线程 ,线程池使用, //启动线程 下载文件 1.线程自己实现方法