文件下载,后端接口,django,flask

 

Django后端接口

def download_excel(request):
    """
    get excel file from url param and upload it to html page for downloading
    :param request: 
    :return: 
    """
    # get file path from a tag url param
    excel_file = request.GET.get("EXCEL")
    file_byte = open(excel_file, "rb")
    response = HttpResponse(file_byte)
    # 设置头信息,告诉浏览器这是个文件
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="attendance.xlsx"'
    return response

 

flask后端接口

def download_get_part():
    """
    把上面下载post返回的excel文件路径,传入进来,用二进制打开,返给浏览器
    :return:
    """
    # todo: 待调试,flask的HttpResponse替代品是啥,
    excel_file = request.args.get("excel")
    # 设置头信息,告诉浏览器这是个文件
    file_byte = open(excel_file, "rb")
    response = Response(file_byte)
    response.headers["Content-Type"] = "application/octet-stream"
    response.headers["Content-Disposition"] = "attachment;filename={}".format(
        "各节点之间的调用关系.xlsx".encode().decode('latin-1')
    )
    return response
    # 这里是另一种方式提供下载文件接口,只有这一行就够了
    # return send_file(excel_file)  # website download file successful

 

posted @ 2022-08-09 10:39  dream-子皿  阅读(120)  评论(0编辑  收藏  举报