- 文件上传 - 普通上传 - 自定义页面上传按钮 - 基于Form做上传 - Ajax上传文件?????
一,上传文件
from django.shortcuts import render,redirect, HttpResponse# Create your views here.def update(request): if request.method == 'GET': return render(request, 'update.html') else: username = request.POST.get('username') img = request.FILES.get('img') print(img) print(username) file_name = img.name file_size = img.size print(file_name, file_size) f = open(img.name, 'wb') for line in img.chunks(): f.write(line) f.close() return HttpResponse("ok")
Title
基于Form上传
Title
from django.shortcuts import render,redirect, HttpResponse# Create your views here.from django import formsfrom django.forms import fieldsclass Upload(forms.Form): user = fields.CharField() img = fields.FileField()def update(request): if request.method == 'GET': obj = Upload() return render(request, 'update.html',{ 'obj':obj}) else: obj = Upload(request.POST, request.FILES) if obj.is_valid(): username = obj.cleaned_data['user'] img = obj.cleaned_data['img'] print(img) print(username) file_name = img.name file_size = img.size print(file_name, file_size) f = open(img.name, 'wb') for line in img.chunks(): f.write(line) f.close() return HttpResponse("ok")