python之文件上传
一、文件上传的几种方式
1、常规文件上传
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>上传列表</title> 6 <link rel="stylesheet" href="/static/bootstrap-4.4.1-dist/css/bootstrap.css"> 7 <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.css"> 8 9 </head> 10 <body> 11 <form action="/upload.html" method="POST" enctype="multipart/form-data"> 12 <input type="text" name = "user"> 13 {# <i class="fa fa-camera-retro fa-5x"></i> fa-5x#} 14 <div style="position: relative"> 15 <a class="fa fa-file-zip-o fa-4x"></a> 16 <input type="file" style="opacity: 0.2;position:absolute;top:0;left: 0;" name = "img" > 17 </div> 18 {# <div class="publish-upload-file check">#} 19 {# <label for="dialog-upload-pic" class="dialog-upload-label">#} 20 {# <input type="file" data-id="0" accept="image/*" data-type="0" name="addFile" style="display:none;" id="dialog-upload-pic">#} 21 {# </label>#} 22 {# </div>#} 23 24 {# <img src="1.jpg" alt="..." name = "img" class="img-rounded">#} 25 <input type="submit" value="提交"> 26 </form> 27 </body> 28 </html>
1 from django.shortcuts import render,HttpResponse 2 from django import forms 3 from django.forms import fields 4 # Create your views here. 5 class uploadforms(forms.Form): 6 user = fields.CharField() 7 img = fields.FileField 8 def upload(request): 9 if request.method =="GET": 10 return render(request,"upload.html") 11 else: 12 user = request.POST.get("user") 13 # img = request.POST.get("img" 14 img = request.FILES.get("img") 15 print() 16 f = open(img.name,'wb') 17 for line in img.chunks(): 18 f.write(line) 19 f.close() 20 return HttpResponse("上传成功")
2、通过Form组件完成上传
from django.shortcuts import render,HttpResponse from django import forms from django.forms import fields # Create your views here. class uploadforms(forms.Form): user = fields.CharField() img = fields.FileField def upload(request): if request.method =="GET": return render(request,"upload.html") else: ###Form组件来实现想 obj = uploadforms(request.POST,request.FILES) if obj.is_valid(): user = obj.cleaned_data["user"] img = obj.cleaned_data["img"] #user = request.POST.get("user") # img = request.POST.get("img" #img = request.FILES.get("img") f = open(img.name,'wb') for line in img.chunks(): f.write(line) f.close() return HttpResponse("上传成功")
3、通过Ajax完成上传
……
二、修改默认上传按钮
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>上传列表</title> 6 <link rel="stylesheet" href="/static/bootstrap-4.4.1-dist/css/bootstrap.css"> 7 <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.css"> 8 9 </head> 10 <body> 11 <form action="/upload.html" method="POST" enctype="multipart/form-data"> 12 <input type="text" name = "user"> 13 {# <i class="fa fa-camera-retro fa-5x"></i> fa-5x#} 14 <div style="position: relative"> 15 <a class="fa fa-file-zip-o fa-4x"></a> 16 <input type="file" style="opacity: 0.2;position:absolute;top:0;left: 0;" name = "img" > 17 </div> 18 {# <div class="publish-upload-file check">#} 19 {# <label for="dialog-upload-pic" class="dialog-upload-label">#} 20 {# <input type="file" data-id="0" accept="image/*" data-type="0" name="addFile" style="display:none;" id="dialog-upload-pic">#} 21 {# </label>#} 22 {# </div>#} 23 24 {# <img src="1.jpg" alt="..." name = "img" class="img-rounded">#} 25 <input type="submit" value="提交"> 26 </form> 27 </body> 28 </html>

浙公网安备 33010602011771号