码迷,mamicode.com
首页 > Web开发 > 详细

Django文件上传

时间:2020-03-14 09:25:30      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:cut   isp   ane   tle   mit   relative   div   html   dmi   

一、普通上传方式

技术图片
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 <form action="/upload.html" method="post" enctype="multipart/form-data">
 9     {% csrf_token %}
10     <p><input type="file" name="uploadFile"></p>
11     <div style="position: relative">
12         <a href="#">自定义上传按钮</a>
13         <input type="submit" value="上传" style="position: absolute;top: 0;left: 0;opacity: 0.2;">
14     </div>
15 </form>
16 </body>
17 </html>
HTML
技术图片
 1 from django.shortcuts import render
 2 from django.shortcuts import redirect
 3 from django.shortcuts import HttpResponse
 4 import os
 5 
 6 # Create your views here.
 7 from django import forms
 8 from django.forms import fields
 9 
10 class myForm(forms.Form):
11     uploadFile=fields.FileField(required=True)
12 
13 def upload(request):
14     if request.method=="GET":
15         return render(request,"upload.html")
16     else:
17         uploadFile=request.FILES.get("uploadFile")
18         f=open(os.path.join(C:/Users/Administrator/PycharmProjects-forWork/Django_upload/static/img,uploadFile.name),"wb")
19         for i in uploadFile.chunks():
20             f.write(i)
21         f.close()
22         return HttpResponse("上传完成")
view函数

二、Form上传方式

技术图片
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 <form action="/upload.html" method="post" enctype="multipart/form-data">
 9     {% csrf_token %}
10     <p><input type="file" name="uploadFile"></p>
11     <div style="position: relative">
12         <a href="#">自定义上传按钮</a>
13         <input type="submit" value="上传" style="position: absolute;top: 0;left: 0;opacity: 0.2;">
14     </div>
15 </form>
16 </body>
17 </html>
HTML
技术图片
 1 from django.shortcuts import render
 2 from django.shortcuts import redirect
 3 from django.shortcuts import HttpResponse
 4 import os
 5 
 6 # Create your views here.
 7 from django import forms
 8 from django.forms import fields
 9 
10 class myForm(forms.Form):
11     uploadFile=fields.FileField(required=True)
12 
13 def upload(request):
14     if request.method=="GET":
15         return render(request,"upload.html")
16     else:
17         obj=myForm(request.POST,request.FILES)
18         if obj.is_valid():
19             uploadFile=obj.cleaned_data["uploadFile"]
20             f=open(os.path.join(C:/Users/Administrator/PycharmProjects-forWork/Django_upload/static/img,uploadFile.name),"wb")
21             for i in uploadFile.chunks():
22                 f.write(i)
23             f.close()
24             return HttpResponse("上传完成")
25         else:
26             return HttpResponse("上传失败")
view函数

 

Django文件上传

标签:cut   isp   ane   tle   mit   relative   div   html   dmi   

原文地址:https://www.cnblogs.com/sun-10387834/p/12490157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!