处理上传的文件

  从request.FILES中获得的真实的文件。这个字典的每个输入都是一个UploadedFile对象——一个上传之后的文件的简单的包装。

你通常会使用下面的几个方法来访问被上传的内容:

  • UploadedFile.read():从文件中读取整个上传的数据。小心整个方法:如果这个文件很大,你把它读到内存中会弄慢你的系统。你可以想要使用chunks()来代替,看下面;
  • UploadedFile.chunks():如果上传的文件足够大需要分块就返回真。默认的这个值是2.5M,当然这个值是可以调节的。

下面是本人写的一个简单的例子:

<form id="myform" enctype="multipart/form-data"><span>选择上传的文件</span><input type="file" id="upload" name="myfiles" multiple><br/><input id="submit" type="button" value="上传">
</form>
<script>$(document).ready(function(){$("#submit").click(function () {var form_data = new FormData();var len = $('#upload')[0].files.length;for(var i =0;i<len;i++) {var file_info = $('#upload')[0].files[i];form_data.append('myfiles', file_info);}$.ajax({url:'upload/',   // 这里对应url.py中的 url(r'upload', views.upload)type:'POST',data: form_data,processData: false,  // tell jquery not to process the datacontentType: false, // tell jquery not to set contentTypesuccess: function(callback) {alert('success');}});});});
</script>
//uploadfile.views.py
from django.http import HttpResponsedef upload(request):if request.method == 'POST':files = request.FILES.getlist('myfiles')for f in files:file = open('file/' + f.name, 'wb+')for chunk in f.chunks():file.write(chunk)file.close()return HttpResponse("OK!")else:return HttpResponse("NOT OK!")
//urls.pyfrom uploadfile import views

在f.chunks()上循环而不是用read()保证大文件不会大量使用你的系统内存。

【Django】用file.chunks()代替file.read()相关推荐

  1. python file.chunks()的使用

    f = open(path+file.name, 'wb+')for c in file.chunks():f.write(c)f.close() 脑抽把chunks前边的file换成了f,导致出现错 ...

  2. libgstreamer-1.0.so.0: cannot open shared object file: No such file or directory

    1. 问题现象 error while loading shared libraries: libgstreamer-1.0.so.0: cannot open shared object file: ...

  3. android file mkdir,android file.mkdir()一直返回false问题

    今天想写本地日志,结果写文件的时候一直失败,报FileNotFound错误,很明显,就是文件创建失败的问题了,debug了一下,发现在创建路径的时候,file.mkdirs()就出问题了(这个方法相对 ...

  4. OSError: libcudart.so.8.0: cannot open shared object file: No such file or directory 解决方法

    OSError: libcudart.so.8.0: cannot open shared object file: No such file or directory 解决方法 检查cuda8是否安 ...

  5. 调用torchtext报错OSError: libtorch_cpu.so: cannot open shared object file: No such file or directory

    环境 torch版本1.4.0 报错 报错信息如题.调用torchtext报错 OSError: libtorch_cpu.so: cannot open shared object file: No ...

  6. File.separator或File.pathSeparator

    本文翻译自:File.separator or File.pathSeparator In the File class there are two strings, separator and pa ...

  7. libopencv_core.so: file not recognized: File format not recognized

    /dependencies/OpenCV348/lib/libopencv_core.so: file not recognized: File format not recognized 解决方法: ...

  8. libpython3.7m.so.1.0: cannot open shared object file: No such file or directory

    安装完python3.7.5之后,pip3.7.5 install psutil --user 报错: libpython3.7m.so.1.0: cannot open shared object ...

  9. ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

    ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory 安装cuda9.1 安 ...

最新文章

  1. dede列表分页php,dede列表页分页英文调用方法
  2. SSM项目整合Quartz
  3. RocketMQ 错误:The broker does not support consumer to filter message by SQL92
  4. 制作网页特效的基本步骤
  5. 第十三次CCFCSP认证(2018年3月)真题碰撞的小球
  6. 服务器虚拟机怎么安装win7系统教程,虚拟机怎么安装系统?VMware虚拟机安装Win7和win10图文详细教程...
  7. 树莓派摄像头——图像 视频采集
  8. 太容易的钱,最让人心慌
  9. 机器智能正在代替人类做的5件事
  10. 3.1 WTL概述,简单使用和ATL概述,简单使用
  11. vue生成随机订单号
  12. 90%的android开发者都会遇到的问题,当sjk_daemon遇见ADB server didn't ACK
  13. 数学建模国赛全过程回顾
  14. 京东有多少钱可以重来?
  15. 了解实时时钟RTC的原理并通过stm32实现STM32的日历读取、设置和输出
  16. 工作站 桌面 服务器,图形工作站也虚拟化,立即让你的工作站也可以远程访问
  17. 人民币首度超过美元!
  18. 超像素经典算法SLIC的代码的深度优化和分析。
  19. 2022年蓝桥杯Python程序设计B组思路和代码分享
  20. linux 设置开机自启动 文件配置开机自启动命令

热门文章

  1. linux路由表命令,linux下路由表详解
  2. python通过librados库通过底层的rados操作ceph的对象存储和块存储
  3. Unity制作即时战略游戏毕设
  4. 现代民机“飞行管理系统(FMS)”的功能和组成
  5. fastjson深度源码解析- 序列化(一) - 序列化基础类型解析
  6. 面试题87:玛瑙项链
  7. 电脑同时访问外网和内网?双路由的详细配置及讲解
  8. Unity 船的控制
  9. java打包成jar_Java程序打包成jar包
  10. 【OpenGL】一、Visual Studio 2019 创建 Windows 桌面程序 ( Visual Studio Installer 安装 C++ 桌面开发库 | 创建桌面程序 )