Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2224400
  • 博文数量: 287
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2130
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-31 14:30
个人简介

自己慢慢积累。

文章分类

全部博文(287)

分类: Python/Ruby

2016-10-28 15:42:44

1、html文件的表单中,需要有 enctype="multipart/form-data"
    这样才能确保后台可以收到

点击(此处)折叠或打开

  1. <form role="form" action="/suggestion_submit/" onsubmit="return validate()" method="POST" enctype="multipart/form-data">
  2. #文件上传部分

  3. 点击(此处)折叠或打开

    1. <div class="form-group">
    2.                             <label for="inputfile">附件</label>
    3.                             <input type="file" id="inputfile" name="inputfile">
    4.                             <label class="help-block">可以上传截图或者文件</label>
    5.                         </div>



2、views.py
     1、request.FILES.get("inputfile", None)  这个inputfile“是跟Html中的上传控件的name对应
     2、E:\\upload 这个目录必须是已经存在的路径。

点击(此处)折叠或打开

  1. def suggestion_submit(request):
  2.     if request.method == 'POST':
  3.         #一些操作。。。。。。。。
  4.         # 保存上传的文件
  5.         myFile = request.FILES.get("inputfile", None)
  6.         destination = open(os.path.join("E:\\upload", myFile.name), 'wb+') # 打开特定的文件进行二进制的写操作
  7.         for chunk in myFile.chunks(): # 分块写入文件
  8.             destination.write(chunk)
  9.         destination.close()
  10.         return render(request, 'index.html',)

阅读(3267) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~