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.效果顯示

 

2.上傳文件按鈕優化

2.1按鈕優化只需在原有upload.html文件中進行相關樣式設置即可,重點設置透明度:opacity:0

 

 1 
<!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.優化後的效果顯示:

 

3.python之Form組件文件上傳(與上述自定義上傳文件的區別在:form上傳文件多了驗證功能)

 

 1 from django.shortcuts import render
 2 from django.shortcuts import HttpResponse
 3 
 4 
 5 # Create your views here.
 6 from django import forms
 7 from django.forms import fields
 8 #from組件形式的文件上傳
 9 class UploadImg(forms.Form):
10     user=fields.CharField()
11     img=fields.FileField()
12     
13     
14 def upload(request):
15     if request.method=='GET':
16         return render(request,'upload.html')
17     else:
18         OBJ=UploadImg(request.POST,request.FILES)
19         if OBJ.is_valid():
20             user=OBJ.cleaned_data['user']
21             img=OBJ.cleaned_data['img']
22             f = open(img.name, 'wb')
23             for line in img.chunks():
24                 f.write(line)
25             f.close()
26         #自定義形式文件上傳
27         # def upload(request):
28         #     if request.method == 'GET':
29         #         return render(request, 'upload.html')
30         #     else:
31         #         user=request.POST.get('user')
32         #         print(user)
33         #         #img是一個對象,包含文件名,文件大小、內容....
34         #         img=request.FILES.get('img')
35         #         print(img)
36         #         print(img.name)
37         #         print(size)
38         #         f = open(img.name, 'wb')
39         #         for line in img.chunks():
40         #             f.write(line)
41         #         f.close()
42         #         return HttpResponse('OK')
43         return HttpResponse('OK')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章