前端代码

$("#id_pullout").click(function () {
//发送ajax请求
$.ajax({
url: '/pullout/', //请求的url
method: "post", //默认get
xhrFields: {
responseType: "arraybuffer",
},
    // 这个选项必须得有,要不文件打不开
data: {'csrfmiddlewaretoken': '{{ csrf_token }}'},
success: function (response) {
        //data接收响应体,必须要有
let blob = new Blob([response], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        //这里。必须得指定格式,每个格式的文件都不一样
});
let link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
<!--link.download = fileName;-->
link.download = '导出数据.xlsx';
link.click();
window.URL.revokeObjectURL(link.href);
}
})
})

后端
def export_datas(request):
try:
    //  这里重新封装了reponse,
response = HttpResponse()
response['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
     
# todo fileName
file_time = time.strftime('%Y-%m-%d %X')
response['fileName'] = file_time+'/'+request.user.username+'.xlsx'
response['Content-Disposition'] = 'attachment;filename="{}.xlsx"'.format(file_time)
response['Accept-Encoding'] = 'gzip, deflate, br'
# print(response.get('fileName'))
outdata=BytesIO()
    // 使用IO操作,把二进制数据存起来
with open('导出文件.xlsx','rb') as f:
outdata.write(f.read())
response.write(outdata.getvalue())
     //这个啥操作没看懂说实话
return response
except Exception as e:
print([e])
return e
这个是魔改了其他大神的东西,有的地方不是很明白,但是起码能用在自己的项目上了
原文链接如下,
https://www.cnblogs.com/jun-zhou/p/16219664.html

django+ajax实现xlsx文件下载功能的更多相关文章

  1. [django]利用xlwt实现文件下载功能

    代码: url.py: url(r'^importmould/$', 'keywork.views.xls_mould', name='xls_mould'), view.py: from djang ...

  2. django+ajax用FileResponse文件下载到浏览器过程中遇到的问题

    问题: 公司的需求是从mongodb中查找数据并下载回本地,但是在将文件从mongodb通过django服务端,然后从django服务端向浏览器下载文件.但是在下载的时候出了些问题.由于是用的ajax ...

  3. JAVA文件下载功能问题解决日志

    今天给报告系统做了个下载功能,遇到了挺多问题,通过查资料一一解决了. 1.首先遇到的问题是:java后台的输出流输出之后,没有任何报错,浏览器端不弹出保存文件的对话框,原本是ajax请求到后台的con ...

  4. Django ajax MYSQL Highcharts<1>

    Another small project with django/Ajax/Mysql/Highcharts. 看下效果图  - delivery dashboard .嘿嘿 是不是还蛮好看的. 废 ...

  5. WebView实现文件下载功能

    WebView控制调用相应的WEB页面进行展示.安卓源码当碰到页面有下载链接的时候,点击上去是一点反应都没有的.原来是因为WebView默认没有开启文件下载的功能,如果要实现文件下载的功能,需要设置W ...

  6. 关于Django Ajax CSRF 认证

    CSRF(Cross-site request forgery跨站请求伪造,也被称为“one click attack”或者session riding,通常缩写为CSRF或者XSRF,是一种对网站的 ...

  7. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  8. Django Ajax序列化与反序列化

    序列化与反序列是最常用的功能,有时我们需要将一个表单组打包成Json格式等然后再提交给服务端,这样可以提高效率节约带框,如下是Django配合Ajax实现的序列化与反序列化,文件上传等操作. Ajax ...

  9. 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题

    问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...

  10. Ajax 生成流文件下载 以及复选框的实现

    JQuery的ajax函数的返回类型只有xml.text.json.html等类型,没有“流”类型,所以我们要实现ajax下载,不能够使用相应的ajax函数进行文件下载.但可以用js生成一个form, ...

随机推荐

  1. 手写reduce()

    function reduce(arr, callBack ,initVal){ if(!Array.isArray(arr) || !arr.length || typeof callBack != ...

  2. k8s master高可用

    每台master都要部署haproxy,keepalived keepalived 配置文件:! Configuration File for keepalivedglobal_defs { rout ...

  3. Docker 查看某一时间段日志

    语法: docker logs [OPTIONS] CONTAINER Options: --details 显示更多的信息 -f, --follow 跟踪实时日志 --since string 显示 ...

  4. S家lic

    1,用1patch里对应的工具patch 2019的s家的工具2,用ocad里的lmgrd和snpslmd替换s家的scl里的3,用scl_keygen产生license,再用fix.bat,添加si ...

  5. NOI1994 删数问题

    [问题描述]键盘输入一个高精度的正整数n(≤240位),去掉其中任意s个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对给定的n和s,寻找一种方案,使得剩下的数字组成的新数最小. 输入 两行第 ...

  6. ie8 不支持 trim方法

    那就自己写一个trim()  String.prototype.trim = function() {                 return this.replace(/(^\s*)|(\s* ...

  7. 启动appium服务时报错,服务不通:Original error: Could not find 'apksigner.jar'

    启动时报错,服务不通:Original error: Could not find 'apksigner.jar' 是因为少了个文件,添加个文件就好了,可以参考下面的帖子. 可以参考这个帖子:http ...

  8. JS斐波那契数列O(n)

    function fibonacci(n) { return fib(n)[n] } var fib=(function(n){ var meo=[0,1] return function(n){ f ...

  9. Vue+Element+Table表格动态跨列文章

    https://my.oschina.net/u/4772459/blog/4699602 如图所示: 1 <template class="SysRole"> 2 & ...

  10. 【Delphi7官方镜像版】delphi_7_ent_en官方镜像 阿里云盘

    [Delphi7官方镜像版]「delphi_7_ent_en官方镜像.iso.exe」https://www.aliyundrive.com/s/Du9C4XfZfwG 点击链接保存,或者复制本段内容 ...