Django文件上传

Posted sun-10387834

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django文件上传相关的知识,希望对你有一定的参考价值。

一、普通上传方式

技术图片
 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文件上传的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Django 中上传文件? [关闭]

Alamofire 文件上传出现错误“JSON 文本未以数组或对象开头,并且允许未设置片段的选项”

django项目中.gitignore文件忽略上传的文件,以及数据库迁移文件到底是否需要上传吗?

Django 文件上传返回无

Django modelForm 中的文件上传

如何在 Django Summernote 中显示编程片段的代码块?