Django获取 请求方式/ 请求路径/ 一个或多个文件/

场景:

         前端: postman接口上传文件

         后台:  django框架,接收文件

def get_other_params(request):

    """
    获取其他参数:  method/path/file
    :param request:
    :return:
    """

    ret_method = request.method
    ret_url = request.get_full_path()

    # 单个文件
    ret_file = request.FILES.get("测试文件")
    # 多个文件
    ret_files = request.FILES.getlist("多个测试文件")

    # 如果不指定路径文件是和manage.py同级的,
    # TODO 这里为什么没吃到settings.py中的os.path.join??????
    # file_path = os.path.join("django_note\\apps\\users", ret_file.name)

    # 保存单个文件
    with open('%s' % ret_file.name, 'wb') as f:
        for i in ret_file.chunks():
            f.write(i)

    # 保存多个文件
    for file in ret_files:
        with open('%s' % file.name, 'wb') as f:
            for i in file.chunks():
                f.write(i)

    print("请求路径为: %s, 请求方法为:%s" % (ret_url, ret_method))
    return HttpResponse("ok")
发布了73 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_42327755/article/details/89349357