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 相关文章推荐
pytyon 带有重复的全排列
Aug 13 Python
Python实现的一个简单LRU cache
Sep 26 Python
python使用BeautifulSoup分页网页中超链接的方法
Apr 04 Python
基于python中pygame模块的Linux下安装过程(详解)
Nov 09 Python
Python Socket使用实例
Dec 18 Python
python机器学习之贝叶斯分类
Mar 26 Python
Python利用sqlacodegen自动生成ORM实体类示例
Jun 04 Python
在django中,关于session的通用设置方法
Aug 06 Python
python文字和unicode/ascll相互转换函数及简单加密解密实现代码
Aug 12 Python
Python列表list操作相关知识小结
Jan 29 Python
使用 Python 在京东上抢口罩的思路详解
Feb 27 Python
利于python脚本编写可视化nmap和masscan的方法
Dec 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
Discuz 模板语句分析及知识技巧
2009/08/21 PHP
php与mysql建立连接并执行SQL语句的代码
2011/07/04 PHP
php获取json数据所有的节点路径
2015/05/17 PHP
php数组比较实现查找连续数的方法
2015/07/29 PHP
很酷的javascript loading效果代码
2008/06/18 Javascript
浅谈javascript的数据类型检测
2010/07/10 Javascript
javascript 事件绑定问题
2011/01/01 Javascript
js将long日期格式转换为标准日期格式实现思路
2013/04/07 Javascript
用表格输出1-1000之间的数字实现代码(附特效)
2013/04/21 Javascript
js判断浏览器版本以及浏览器内核的方法
2015/01/20 Javascript
关于React动态加载路由处理的相关问题
2019/01/07 Javascript
vue-video-player实现实时视频播放方式(监控设备-rtmp流)
2020/08/10 Javascript
js实现表格单列按字母排序
2020/08/12 Javascript
自己使用总结Python程序代码片段
2015/06/02 Python
Python开发SQLite3数据库相关操作详解【连接,查询,插入,更新,删除,关闭等】
2017/07/27 Python
python中的字典操作及字典函数
2018/01/03 Python
python定向爬取淘宝商品价格
2018/02/27 Python
将python代码和注释分离的方法
2018/04/21 Python
对Python实现累加函数的方法详解
2019/01/23 Python
导入tensorflow时报错:cannot import name 'abs'的解决
2019/10/10 Python
Python3自动生成MySQL数据字典的markdown文本的实现
2020/05/07 Python
Cotton On美国网站:澳洲时装连锁品牌
2016/10/25 全球购物
丝芙兰墨西哥官网:Sephora墨西哥
2020/05/30 全球购物
PHP数据运算类型都有哪些
2013/11/05 面试题
大学生村官事迹材料
2014/01/21 职场文书
淘宝中秋节活动方案
2014/01/31 职场文书
优秀学生干部推荐材料
2014/02/03 职场文书
应届毕业生通用的自荐书范文
2014/02/07 职场文书
财务科科长岗位职责
2014/03/10 职场文书
学校党的群众路线教育实践活动总结报告
2014/07/03 职场文书
2014年检验员工作总结
2014/11/19 职场文书
六一文艺汇演开幕词
2015/01/29 职场文书
五年级作文之学校的四季
2019/12/05 职场文书
Django实现聊天机器人
2021/05/31 Python
简单介绍Python的第三方库yaml
2021/06/18 Python
mysql获取指定时间段中所有日期或月份的语句(不设存储过程,不加表)
2021/06/18 MySQL