json代码
$("#download").on("click", function () {
location.href="/cexcel/download/?download="+"yes"+"&excel_file="+encodeURIComponent(data["excel_file"])+"&excel_name="+encodeURIComponent(data["excel_name"]);
})
django代码
def exceldownload(request):
if request.method == "GET" and request.GET.get('download') == "yes":
excel_file = request.GET["excel_file"]
excel_name = request.GET["excel_name"]
file = open(excel_file, 'rb')
response = FileResponse(file)
# response['Content-Type'] = 'application/vnd.ms-excel' #xls
response['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' #xlsx
response['Content-Disposition'] = 'attachment;filename="%s"' % excel_name
return response