首页 > 代码库 > django 的文件上传
django 的文件上传
template html(模板文件):
<form enctype="multipart/form-data" method="POST" action="/address/upload/"> <input type="file" name="file" /> <br /> <input type="submit" value=http://www.mamicode.com/"上传文件" /> </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()
from django.db import modelsclass UploadFile(models.Model): userid = models.CharField(max_length = 30) file = models.FileField(upload_to = ‘./upload/‘) date = models.DateTimeField(auto_now_add=True)
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
django 的文件上传
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。