Django接收照片储存文件的实例代码

作者:PythonNew_Mr.Wang 时间:2022-06-01 09:05:29 

后端:


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>

下面在看下在Django中接收文件并存储

首先是一个views函数的例子 


def get_user_profiles(request):
 if request.method == 'POST':
     myFile = request.FILES.get("filename", None)
     if myFile:
       dir = os.path.join(os.path.join(BASE_DIR, 'static'),'profiles')
       destination = open(os.path.join(dir, myFile.name),
                 'wb+')
       for chunk in myFile.chunks():
         destination.write(chunk)
       destination.close()
     return HttpResponse('ok')

这是一个简单的接收客户端上传的头像文件并保存的例子,应该看过这个就已经大体会使用接收文件了

但是这里的filename是客户端上传的文件名,也可能是像下面这样的表单 

<input type="file" name="filename" />

如果不知道固定上传的文件名,想要客户端上传什么文件就以其上传的名字命名可以这么写


def get_user_profiles(request):
 if request.method == 'POST':
   if request.FILES:
     myFile =None
     for i in request.FILES:
       myFile = request.FILES[i]
     if myFile:
       dir = os.path.join(os.path.join(BASE_DIR, 'static'),'profiles')
       destination = open(os.path.join(dir, myFile.name),
                 'wb+')
       for chunk in myFile.chunks():
         destination.write(chunk)
       destination.close()
     return HttpResponse('ok')

不过这个是通过输出request.FILES试出来的,不知道是否有更合适的方法。

来源:https://www.cnblogs.com/wanghong1994/p/12431691.html

标签:django,接收,储存
0
投稿

猜你喜欢

  • python3环境搭建过程(利用Anaconda+pycharm)完整版

    2022-01-16 06:04:47
  • 使用django的objects.filter()方法匹配多个关键字的方法

    2022-04-08 06:11:20
  • PyCharm上安装Package的实现(以pandas为例)

    2021-09-21 12:26:30
  • 教你使用SQL Server数据库进行网络链接

    2009-01-13 13:41:00
  • python实现高斯投影正反算方式

    2022-11-17 08:58:19
  • 最大限度优化你的Asp性能

    2007-10-01 18:04:00
  • python中print()函数的“,”与java中System.out.print()函数中的“+”功能详解

    2021-12-18 10:27:02
  • python UDP(udp)协议发送和接收的实例

    2022-09-01 10:17:02
  • JavaScript+html实现前端页面随机二维码验证

    2024-04-19 09:48:59
  • so easy!10行代码写个"狗屁不通"文章生成器功能

    2023-03-19 06:53:30
  • python抓取京东小米8手机配置信息

    2021-10-12 15:55:32
  • python如何实现质数求和

    2023-03-02 20:17:24
  • Go语言基础go fmt命令使用示例详解

    2024-01-31 11:37:34
  • 基于mysql全文索引的深入理解

    2024-01-15 15:28:41
  • Python 相对路径报错:"No such file or directory"'原因及解决方法

    2021-08-12 05:34:00
  • 使用python画出逻辑斯蒂映射(logistic map)中的分叉图案例

    2023-05-19 13:05:18
  • python调用api实例讲解

    2023-09-16 02:32:33
  • 将imagenet2012数据为tensorflow的tfrecords格式并跑验证的详细过程

    2024-01-01 12:04:35
  • Python爬虫信息输入及页面的切换方法

    2023-08-02 17:33:33
  • 基于python二叉树的构造和打印例子

    2023-05-16 15:35:45
  • asp之家 网络编程 m.aspxhome.com