django 实现文件下载功能

以下是一个例子
先上接口:
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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值