码迷,mamicode.com
首页 > Web开发 > 详细

django 的文件上传

时间:2016-09-17 00:37:08      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:

template html(模板文件):

<form enctype="multipart/form-data" method="POST" action="/address/upload/"> 
   <input type="file" name="file" />
   <br /> 
   <input type="submit" value="上传文件" /> 
</form>

a、自定义上传(建议用自定义的)

def index(request):
if request.method == ‘POST‘:

obj = request.FILES.get(‘file‘)
import os
ppp = os.path.join(‘static‘,‘img‘,obj.name)

f = open(ppp, ‘wb‘)
for chunk in obj.chunks():
f.write(chunk)
f.close()
return HttpResponse(request,上传成功‘)
# return render(request, ‘file.html‘)
else:
return render(request,‘index.html‘)

b、Form上传文件实例

技术分享
class FileForm(forms.Form):
    ExcelFile = forms.FileField()
form

 

技术分享
from django.db import models

class UploadFile(models.Model):
    userid = models.CharField(max_length = 30)
    file = models.FileField(upload_to = ./upload/)
    date = models.DateTimeField(auto_now_add=True)
models

 

技术分享
def UploadFile(request):
    uf = AssetForm.FileForm(request.POST,request.FILES)
    if uf.is_valid():
            upload = models.UploadFile()
            upload.userid = 1
            upload.file = uf.cleaned_data[ExcelFile]
            upload.save()
            
            print upload.file
View Code

 

django 的文件上传

标签:

原文地址:http://www.cnblogs.com/renfanzi/p/5840077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!