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使用面向对象方式创建线程实现12306售票系统
Dec 24 Python
Python中的迭代器与生成器高级用法解析
Jun 28 Python
TensorFlow利用saver保存和提取参数的实例
Jul 26 Python
Python从数据库读取大量数据批量写入文件的方法
Dec 10 Python
Python Pywavelet 小波阈值实例
Jan 09 Python
Python利用itchat库向好友或者公众号发消息的实例
Feb 21 Python
django settings.py 配置文件及介绍
Jul 15 Python
Python操作Mongodb数据库的方法小结
Sep 10 Python
详解PyQt5中textBrowser显示print语句输出的简单方法
Aug 07 Python
Python用SSH连接到网络设备
Feb 18 Python
Python实现8种常用抽样方法
Jun 27 Python
用Python仅20行代码编写一个简单的端口扫描器
Apr 08 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
Erlang的运算符(比较运算符,数值运算符,移位运算符,逻辑运算符)
2012/07/23 PHP
PHP后期静态绑定之self::限制实例分析
2018/12/21 PHP
jQuery 借助插件Lavalamp实现导航条动态美化效果
2013/09/27 Javascript
js 3种归并操作的实例代码
2013/10/30 Javascript
原生javascript实现隔行换色
2015/01/04 Javascript
JS实现字符串转日期并比较大小实例分析
2015/12/09 Javascript
深入浅析JavaScript函数前面的加号和叹号
2016/07/09 Javascript
深入剖析JavaScript面向对象编程
2016/07/12 Javascript
AngularJS中run方法的巧妙运用
2017/01/04 Javascript
jquery实现折叠菜单效果【推荐】
2017/03/08 Javascript
微信小程序学习之数据处理详解
2017/07/05 Javascript
JavaScript中的ES6 Proxy的具体使用
2019/06/16 Javascript
微信小程序实现弹出菜单动画
2019/06/21 Javascript
JavaScript this使用方法图解
2020/02/04 Javascript
Vue常用的全选/反选的示例代码
2020/02/19 Javascript
python访问sqlserver示例
2014/02/10 Python
python发布模块的步骤分享
2014/02/21 Python
Python实现压缩与解压gzip大文件的方法
2016/09/18 Python
Python中常见的异常总结
2018/02/20 Python
Python切片操作实例分析
2018/03/16 Python
Python @property使用方法解析
2019/09/17 Python
python单例模式原理与创建方法实例分析
2019/10/26 Python
如何使用Python发送HTML格式的邮件
2020/02/11 Python
python从ftp获取文件并下载到本地
2020/12/05 Python
用60行代码实现Python自动抢微信红包
2021/02/04 Python
LTD Commodities:礼品,独特发现,家居装饰,家用器皿
2017/08/11 全球购物
英国豪华真皮和布艺沙发销售网站:Darlings of Chelsea
2018/01/05 全球购物
英国鹦鹉店:Parrot Essentials
2018/12/03 全球购物
HQhair美国/加拿大:英国化妆品、美容及美发产品商城
2019/04/15 全球购物
MYPROTEIN澳大利亚官方网站:欧洲运动营养品牌
2019/06/26 全球购物
结婚周年感言
2014/02/24 职场文书
电子商务优秀毕业生求职信
2014/07/11 职场文书
2015秋季新学期开学寄语
2015/05/28 职场文书
《狼王梦》读后感:可怜天下父母心
2019/11/01 职场文书
关于PostgreSQL JSONB的匹配和交集问题
2021/09/14 PostgreSQL
python 使用pandas读取csv文件的方法
2022/12/24 Python