• 【Django】接收照片,储存文件 前端代码


    后端:
    from rest_framework.views import APIView
    from car import settings
    from django.shortcuts import render, redirect, HttpResponse
    from dal import models
    from django.http import JsonResponse
    import os
    
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    class Image(APIView):
    
        def post(self, request):
            file_obj = request.FILES.get('send',None)
    
            print("file_obj",file_obj.name)
    
            file_path = os.path.join(BASE_DIR, 'media', 'user/img', file_obj.name)
    
            print("file_path", file_path)
    
            with open(file_path, 'w') as f:
                for chunk in file_obj.chunks():
                    f.write(chunk)
    
            message = {}
            message['code'] = 200
    
            return JsonResponse(message)

    前端ajax:

        <form method="post" action="/upload/" enctype="multipart/form-data" target="ifm1">
    
    
            <input type="file" name="send"/>
    
    
            <input type="submit" value="Form表单提交"/>
        </form>
  • 相关阅读:
    微服务
    JNDI Tomcat
    JNDI
    依赖倒置原则
    mac下为gdb创建证书赋权其调试其它应用
    sed相关
    关于autoconf
    mac相关
    about gnu bash shell
    关于gcc
  • 原文地址:https://www.cnblogs.com/wanghong1994/p/12431691.html
Copyright © 2020-2023  润新知