我想在django上传文件得时候加一个类似进度条的东西。在上传的时候让研发人员知道上传进度的百分比
这是views.py文件信息
root@VM-34-147-ubuntu:/home/ubuntu/mysite4/sxtx# cat views.py
#coding=utf-8
from django.shortcuts import render,render_to_response
from django import forms
from django.http import HttpResponse
from sxtx.models import User
# Create your views here.
class UserForm(forms.Form):
username = forms.CharField()
headImg = forms.FileField()
def register(request):
if request.method == "POST":
uf = UserForm(request.POST,request.FILES)
if uf.is_valid():
#获取表单信息
username = uf.cleaned_data['username']
headImg = uf.cleaned_data['headImg']
#写入数据库
user = User()
user.username = username
user.headImg = headImg
user.save()
return HttpResponse('上传成功!')
else:
uf = UserForm()
return render_to_response('register.html',{'uf':uf})
这是models.py文件信息
root@VM-34-147-ubuntu:/home/ubuntu/mysite4/sxtx# cat models.py
from django.db import models
# Create your models here.
class User(models.Model):
username = models.CharField(max_length = 30)
headImg = models.FileField(upload_to = './package/sxtx')
def __unicode__(self):
return self.username
这是register.html文件信息
root@VM-34-147-ubuntu:/home/ubuntu/mysite4/sxtx/templates# cat register.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<h1>上传</h1>
<form method="post" enctype="multipart/form-data" >
{{uf.as_p}}
<input type="submit" value="ok"/>
</form>
</body>
</html
这个我在使用flask开发项目的过程中遇到过,跟后台关系不大,主要是前端js实现,具体使用了XMLHttpRequest的ProgressEvent事件,可以参考MDN中的Using XMLHttpRequest