django 实现文件下载功能
2017-07-21 12:54
288 查看
以下是一个例子
先上接口:
url(r'^api/download_excel/$', APIs.DownloadScoreExcel),
# download score excel def DownloadScoreExcel(aRequest): file_name = aRequest.GET['file_name'] file = os.getcwd() + '/tAPP/tAPPFile/' + file_name # 文件位置 from django.utils.encoding import smart_str
# mimetype 变成了 content_type 在 django 1.7以及更高的办恩重 response = HttpResponse(content_type='application/force-download') # mimetype is replaced by content_type for django 1.7 response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name) response['X-Sendfile'] = smart_str(file)
response.write(open(file,'rb').read())
return response
在flask中下载文件很简单:
# return excel @app.route("/download/<id>", methods=['GET']) def down_excel(id): #id 是文件名
path = os.getcwd() + "/download/" #我的文件存放的路径 if os.path.isfile(path + id): return send_from_directory(path, id) else: return jsonify(status='没有你要的文件', code=404)
相关文章推荐
- Django实现下载文件功能
- Django 实现下载文件功能的示例
- django实现下载文件功能
- Django 实现下载文件功能
- Django中实现文件下载功能
- .Net实现文件下载功能
- .Net Compact Framework实现文件下载功能
- 把文件以二进制格式或base64字符串形式保存到数据库中,并实现下载功能源码
- 利用FTPClient类实现文件的上传下载功能
- 闲来无事之--记用BAT(批处理脚本)实现文件下载功能(续)
- 用Jsp来实现文件下载功能的几种方式
- ASP隐藏真实文件的下载功能实现代码
- 用JSP来实现文件下载功能的几种方式
- 用C#怎么实现文件下载功能
- 用jsp实现直接下载文件的功能
- 实现文件下载功能
- 用Visual C#实现文件下载功能
- 用Visual C#实现文件下载功能
- linux下实现ftp匿名用户的上传和下载文件功能
- 利用FTPClient类实现文件的上传下载功能