django+js實現下載

json代碼javascript

$("#download").on("click", function () {
    location.href="/cexcel/download/?download="+"yes"+"&excel_file="+encodeURIComponent(data["excel_file"])+"&excel_name="+encodeURIComponent(data["excel_name"]);
})

 

django代碼java

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
相關文章
相關標籤/搜索