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


Posted in Python onMarch 07, 2020

后端:

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试出来的,不知道是否有更合适的方法。

总结

到此这篇关于Django接收照片储存文件的实例代码 的文章就介绍到这了,更多相关Django储存文件内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
用python登录Dr.com思路以及代码分享
Jun 25 Python
pytorch + visdom 处理简单分类问题的示例
Jun 04 Python
Flask配置Cors跨域的实现
Jul 12 Python
在Python中append以及extend返回None的例子
Jul 20 Python
python将三维数组展开成二维数组的实现
Nov 30 Python
Python实现点云投影到平面显示
Jan 18 Python
tensorflow实现对张量数据的切片操作方式
Jan 19 Python
Python request中文乱码问题解决方案
Sep 17 Python
python实现文件分片上传的接口自动化
Nov 19 Python
Python中Pyspider爬虫框架的基本使用详解
Jan 27 Python
python中sys模块的介绍与实例
Apr 17 Python
详解OpenCV获取高动态范围(HDR)成像
Apr 29 Python
Python实现对adb命令封装
Mar 06 #Python
对Python中 \r, \n, \r\n的彻底理解
Mar 06 #Python
python去除删除数据中\u0000\u0001等unicode字符串的代码
Mar 06 #Python
mac在matplotlib中显示中文的操作方法
Mar 06 #Python
python数据类型可变不可变知识点总结
Mar 06 #Python
python GUI库图形界面开发之PyQt5信号与槽的高级使用技巧装饰器信号与槽详细使用方法与实例
Mar 06 #Python
python GUI库图形界面开发之PyQt5信号与槽的高级使用技巧(自定义信号与槽)详解与实例
Mar 06 #Python
You might like
PHP Document 代码注释规范
2009/04/13 PHP
PHP实现对png图像进行缩放的方法(支持透明背景)
2015/07/15 PHP
针对thinkPHP5框架存储过程bug重写的存储过程扩展类完整实例
2018/06/16 PHP
关于二级域名下使用一级域名下的COOKIE的问题
2011/11/07 Javascript
javascript自动给文本url地址增加链接的方法分享
2014/01/20 Javascript
Javascript学习笔记之 对象篇(一) : 对象的使用和属性
2014/06/24 Javascript
js控制文本框输入的字符类型方法汇总
2015/06/19 Javascript
移除AngularJS下URL中的#字符的方法
2015/06/19 Javascript
jQuery动画效果相关方法实例分析
2015/12/31 Javascript
详解javascript事件冒泡
2016/01/09 Javascript
使用jQuery判断Div是否在可视区域的方法 判断div是否可见
2016/02/17 Javascript
微信小程序 页面传参实例详解
2016/11/16 Javascript
jQuery ajax动态生成table功能示例
2017/06/14 jQuery
详谈js中标准for循环与foreach(for in)的区别
2017/11/02 Javascript
Vue自定义过滤器格式化数字三位加一逗号实现代码
2018/03/23 Javascript
详解基于Node.js的HTTP/2 Server实践
2018/05/31 Javascript
基于AngularJS拖拽插件ngDraggable.js实现拖拽排序功能
2019/04/02 Javascript
[04:13]2018国际邀请赛典藏宝瓶Ⅱ饰品一览
2018/07/21 DOTA
举例讲解Python中的算数运算符的用法
2015/05/13 Python
分析Python中设计模式之Decorator装饰器模式的要点
2016/03/02 Python
对Python中列表和数组的赋值,浅拷贝和深拷贝的实例讲解
2018/06/28 Python
Python批处理删除和重命名文件夹的实例
2018/07/11 Python
python画图——实现在图上标注上具体数值的方法
2019/07/08 Python
Python Django view 两种return的实现方式
2020/03/16 Python
python判断是空的实例分享
2020/07/06 Python
python字典通过值反查键的实现(简洁写法)
2020/09/30 Python
使用豆瓣源来安装python中的第三方库方法
2021/01/26 Python
CSS3 animation实现简易幻灯片轮播特效
2016/09/27 HTML / CSS
C#笔试题集合
2013/06/21 面试题
大学生毕业的自我鉴定
2013/11/13 职场文书
行政助理的职责
2013/11/14 职场文书
收款授权委托书
2014/10/02 职场文书
Pytorch distributed 多卡并行载入模型操作
2021/06/05 Python
浅谈@Value和@Bean的执行顺序问题
2021/06/16 Java/Android
WebRTC记录音视频流(web技术分享)
2022/02/24 Javascript
redis数据一致性的实现示例
2022/03/18 Redis