Django中实现文件上传功能
2016-07-15 10:19
459 查看
在web开发中,文件上传与下载是常见的功能,在Django中实现文件的上传与下载也非常简单,实现步骤与其他功能实现类似,1. 新建一个模板,2. 编写模板对应的view函数,3. 配置view与模板关联的url。具体实现如下:
1. 新建一个模板:
新建一个用于上传文件的模板(本文的模板是基于bootstrap的),内容如下:
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form class="form-inline" role="form" method="post" enctype="multipart/form-data" accept-charset="utf-8">
<div class="form-group">
<input type="file" name="file">
</div>
<div class="form-group">
<input type="submit" value="上传文件">
</div>
</form>
</div>
</div>
注意:这个Form表单中必须有属性enctype="multipart/form-data",这样当request方法是POST时,处理这个form的view中才能接受到request.FILES中的文件数据,可以通过request.FILES['file']来存取。如果不设置这个属性,request.FILES则为空。
2. 编写模板对应的view函数
def upload(request):
if request.method=="POST":
handle_upload_file(request.FILES['file'],str(request.FILES['file']))
return HttpResponse('Successful') #此处简单返回一个成功的消息,在实际应用中可以返回到指定的页面中
return render_to_response('course/upload.html')
def handle_upload_file(file,filename):
path='media/uploads/' #上传文件的保存路径,可以自己指定任意的路径
if not os.path.exists(path):
os.makedirs(path)
with open(path+filename,'wb+')as destination:
for chunk in file.chunks():
destination.write(chunk)
3. 配置相应的URL;
在urls.py文件中配置相应的url,
url(r'^upload/$',views.upload,name='upload'),
经过上述三个步骤后,我们就写好文件上传的功能,下面测试一下吧:
启动开发服务器后,访问相应的upload页面,页面如下:
点击【选择文件】按钮,打开想要上传的文件,然后点击【上传文件】按钮,就可以将文件上传到指定的文件夹中了。
相关的参考文章:
http://www.cnblogs.com/linjiqin/p/3731751.html http://my.oschina.net/yushulx/blog/469802
1. 新建一个模板:
新建一个用于上传文件的模板(本文的模板是基于bootstrap的),内容如下:
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form class="form-inline" role="form" method="post" enctype="multipart/form-data" accept-charset="utf-8">
<div class="form-group">
<input type="file" name="file">
</div>
<div class="form-group">
<input type="submit" value="上传文件">
</div>
</form>
</div>
</div>
注意:这个Form表单中必须有属性enctype="multipart/form-data",这样当request方法是POST时,处理这个form的view中才能接受到request.FILES中的文件数据,可以通过request.FILES['file']来存取。如果不设置这个属性,request.FILES则为空。
2. 编写模板对应的view函数
def upload(request):
if request.method=="POST":
handle_upload_file(request.FILES['file'],str(request.FILES['file']))
return HttpResponse('Successful') #此处简单返回一个成功的消息,在实际应用中可以返回到指定的页面中
return render_to_response('course/upload.html')
def handle_upload_file(file,filename):
path='media/uploads/' #上传文件的保存路径,可以自己指定任意的路径
if not os.path.exists(path):
os.makedirs(path)
with open(path+filename,'wb+')as destination:
for chunk in file.chunks():
destination.write(chunk)
3. 配置相应的URL;
在urls.py文件中配置相应的url,
url(r'^upload/$',views.upload,name='upload'),
经过上述三个步骤后,我们就写好文件上传的功能,下面测试一下吧:
启动开发服务器后,访问相应的upload页面,页面如下:
点击【选择文件】按钮,打开想要上传的文件,然后点击【上传文件】按钮,就可以将文件上传到指定的文件夹中了。
相关的参考文章:
http://www.cnblogs.com/linjiqin/p/3731751.html http://my.oschina.net/yushulx/blog/469802
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法