python 文件上传本地服务器

时间:2023-03-09 15:22:00
python 文件上传本地服务器

1:python之上传文件

1.1.url代码

 """untitled1222 URL Configuration

 The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('upload/',views.upload),
]

1.2.views代码

 from django.shortcuts import render
from django.shortcuts import HttpResponse # Create your views here.
def upload(request):
if request.method=='GET':
return render(request,'upload.html')
else:
user=request.POST.get('user')
print(user)
#img是一个对象,包含文件名,文件大小、内容....
img=request.FILES.get('img')
print(img)
print(img.name)
print(img.size) #上传到本地服务器
f=open(img.name,'wb')
for line in img.chunks():
f.write(line)
f.close() return HttpResponse('OK')

1.3.templates中upload.html

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/upload/" method="post" enctype="multipart/form-data">
<input type="text" name="user">
<input type="file" name="img">
<input type="submit" value="提交">
</form>
</body>
</html>

1.4.效果显示

python 文件上传本地服务器

2.上传文件按钮优化

2.1按钮优化只需在原有upload.html文件中进行相关样式设置即可,重点设置透明度:opacity:0

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/upload/" method="post" enctype="multipart/form-data">
<input type="text" name="user">
<div style="position: relative;">
<a>上传</a>
<input type="file" name="img" style="opacity: 0.2;position:absolute; top:0;left: 0;">
</div>
<input type="submit" value="提交">
</form>
<script src="/static/jquery-3.3.1.min.js"></script>
</body>
</html>

2.2.优化后的效果显示:

python 文件上传本地服务器

3.python之Form组件文件上传(与上述自定义上传文件的区别在:form上传文件多了验证功能)

 from django.shortcuts import render
from django.shortcuts import HttpResponse # Create your views here.
from django import forms
from django.forms import fields
#from组件形式的文件上传
class UploadImg(forms.Form):
user=fields.CharField()
img=fields.FileField() def upload(request):
if request.method=='GET':
return render(request,'upload.html')
else:
OBJ=UploadImg(request.POST,request.FILES)
if OBJ.is_valid():
user=OBJ.cleaned_data['user']
img=OBJ.cleaned_data['img']
f = open(img.name, 'wb')
for line in img.chunks():
f.write(line)
f.close()
#自定义形式文件上传
# def upload(request):
# if request.method == 'GET':
# return render(request, 'upload.html')
# else:
# user=request.POST.get('user')
# print(user)
# #img是一个对象,包含文件名,文件大小、内容....
# img=request.FILES.get('img')
# print(img)
# print(img.name)
# print(size)
# f = open(img.name, 'wb')
# for line in img.chunks():
# f.write(line)
# f.close()
# return HttpResponse('OK')
return HttpResponse('OK')