py文件:

from django.utils.http import urlquote
from rest_framework.views import APIView
from django.shortcuts import render, redirect, HttpResponse
from dal import models
from django.http import JsonResponse, FileResponse, StreamingHttpResponse
import os import xlwt BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # C:\Users\user\Desktop\DownTest class fileShow(APIView): def get(self, request): message = {}
userinfo = models.UserInfo.objects.all() workbook = xlwt.Workbook(encoding='utf-8') # 新建工作簿\ sheet1 = workbook.add_sheet("用户表") # 新建sheet
sheet1.write(0, 0, "ID") # 第1行第1列数据
sheet1.write(0, 1, "用户名") # 第1行第1列数据
sheet1.write(0, 2, "密码") # 第1行第1列数据
sheet1.write(0, 3, "创建时间") # 第1行第1列数据 path = BASE_DIR + "\\utils\\username.xlsx" # 储存路径 excel_row = 1
for obj in userinfo:
data_id = obj.id
data_username = obj.username
data_password = obj.password
data_time = obj.create_time sheet1.write(excel_row, 0, data_id)
sheet1.write(excel_row, 1, data_username)
sheet1.write(excel_row, 2, data_password)
sheet1.write(excel_row, 3, str(data_time))
excel_row += 1 workbook.save(path) file = open(path,'rb') # 字符串替换成文件
print("file",file.name)
# file_names = file.name.split('/')[-1]
# print("file_names",file_names) response = FileResponse(file)
response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = "attachment;filename={}".format(urlquote(path)) # 字符串替换成下载文件
print(response)
return response

html的ajax请求:

<button id="up">下载文件</button>  一个按钮

请求:

// 下载文件
$("#up").on("click", function () {
$.ajax({
url: "down/",
type: "get",
data: {},
success: function (data) {
var $a = $('<a></a>');
$a.attr("href", "http://127.0.0.1:8000/down/"); # 红色的是你下载路径
$("body").append($a);
$a[0].click();
$a.remove();
} })

最新文章

  1. 架构设计:负载均衡层设计方案(3)——Nginx进阶
  2. WEB安全性测试
  3. 用OMT方法建立其分析模型: 本大学基于网络的课程注册系统。
  4. 管理系统-------------SSH框架书写登录和显示用户
  5. ural 1070. Local Time
  6. nullcon HackIM2016 -- Programming Question 3
  7. Yii源码阅读笔记(七)
  8. 转:ORACLE制造方法的比较
  9. JAVA GUI学习 - JTable表格组件学习_A ***
  10. ubuntu12.04 安装和配置jdk1.7
  11. 任务调度之持久化(基于Quartz.net)
  12. 简单的RTSP消息交互过程
  13. while死循环导致的内存溢出
  14. C# 显示纯文本对齐封装(控制显示字体长度)
  15. CSS三种样式
  16. Java面试题复习笔记(Web方向)
  17. DAY4:简单购物系统
  18. CnPack实用功能推荐
  19. Vue 项目架构设计与工程化实践
  20. Stiring公式证明

热门文章

  1. Hadoop集群搭建(五)~搭建集群
  2. iview Checkbox 多选框 v-model 赋值方法 this.innerValueArr = [this.previousValue]
  3. 一、create-react-app的安装及使用
  4. maven中的pom配置文件一——spring,mybatis,oracle,jstl,json,文件上传
  5. Jmeter之JSON提取器应用
  6. mybatis高级应用(四)_逆向工程
  7. Mysql数据库设置权限
  8. hdu6026 dijkstra
  9. CF1327C Game with Chips 题解
  10. [UWP]使用AlphaMaskEffect提升故障艺术动画的性能(顺便介绍怎么使用性能探测器分析UWP程序)