分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

Django处理文件上传File Uploads

发布时间:2023-09-06 01:54责任编辑:白小东关键词:文件上传

HttpRequest.FILES

表单上传的文件对象存储在类字典对象request.FILES中,表单格式需为multipart/form-data

1
2
<form enctype="multipart/form-data"method="post"action="/foo/">
<input type="file"name="image"/>

request.FILES中的键来自于表单中的<input type="file" name="" />的name值:

1
img=request.FILES[‘image‘]

request.FILES中的值均为UploadedFile类文件对象。

UploadedFile

UploadedFile是类文件对象,具有以下方法和属性:

UploadedFile.read()

读取整个上传文件的数据,文件较大时慎用。

UploadedFile.multiple_chunks(chunk_size=None)

判断文件是否足够大,一般为2.5M

UploadedFile.chunks(chunk_size=None)

返回一个生成器对象,当multiple_chunks()为True时应该使用这个方法来代替read().

UploadedFile.name

上传文件的name。

UploadedFile.size

上传文件的大小。

UploadedFile.content_type

上传文件时的content_type报头,例如(e.g. text/plain or application/pdf).

UpladedFile.charset

编码

存储文件

想将上传的文件存储在本地时:

1
2
3
4
f=request.FILES[‘image‘]
withopen(‘some/file/name.txt‘,‘wb+‘) as destination:
forchunkinf.chunks():
destination.write(chunk)

使用Form处理上传文件

也可以使用django自带的form来处理上传文件。

先创建带FileFiled或者ImageFiled的Form:

1
2
3
4
5
6
# In forms.py...
from djangoimportforms
classUploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()

用Form处理:

1
>>> f =UploadFileFormt(request.POST, request.FILES)

view函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from django.httpimportHttpResponseRedirect
from django.shortcutsimportrender_to_response
from .formsimportUploadFileForm
def handle_uploaded_file(f):
withopen(‘some/file/name.txt‘,‘wb+‘) as destination:
forchunkinf.chunks():
destination.write(chunk)
def upload_file(request):
ifrequest.method ==‘POST‘:
form = UploadFileForm(request.POST, request.FILES)
ifform.is_valid():
handle_uploaded_file(request.FILES[‘file‘])
returnHttpResponseRedirect(‘/success/url/‘)
else:
form = UploadFileForm()
returnrender_to_response(‘upload.html‘, {‘form‘: form})

使用Model处理上传文件

如果创建了一个带FileField或者ImageField域的Model,需要将上传文件存储到Model的FileFIeld域。

比如,使用nicEdit文本编辑器时,需要存储上传的文件,建立Model:

1
2
3
4
from django.dbimportmodels
classNicEditImage(models.Model):
image = models.ImageField(upload_to=‘nicedit/%Y/%m/%d‘)

创建ModelForm:

1
2
3
4
5
from djangoimportforms
classNicEditImageForm(forms.ModelForm):
classMeta:
model = NicEditImage

view:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def upload(request):
ifnot request.user.is_authenticated():
json = simplejson.dumps({
‘success‘: False,
‘errors‘: {‘__all__‘:‘Authentication required‘}})
returnHttpResponse(json, mimetype=‘application/json‘)
form = NicEditImageForm(request.POST or None, request.FILES or None)
ifform.is_valid():
image = form.save()#保存Form和Model
json = simplejson.dumps({
‘success‘: True,
‘upload‘: {
‘links‘: {
‘original‘: image.image.url},
‘image‘: {
‘width‘: image.image.width,
‘height‘: image.image.height}
}
})
else:
json = simplejson.dumps({
‘success‘: False,‘errors‘: form.errors})
returnHttpResponse(json, mimetype=‘application/json‘)

当然也可以手动存储文件到Model的文件域:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from django.httpimportHttpResponseRedirect
from django.shortcutsimportrender
from .formsimportUploadFileForm
from .modelsimportModelWithFileField
def upload_file(request):
ifrequest.method ==‘POST‘:
form = UploadFileForm(request.POST, request.FILES)
ifform.is_valid():
instance = ModelWithFileField(file_field=request.FILES[‘file‘])#保存文件到FileField域
instance.save()
returnHttpResponseRedirect(‘/success/url/‘)
else:
form = UploadFileForm()
returnrender(request,‘upload.html‘, {‘form‘: form})

不使用Form处理

想获得更大自由度时,可以全手动处理。

1
2
3
4
5
6
from django.dbimportmodels
classCar(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=5, decimal_places=2)
photo = models.ImageField(upload_to=‘cars‘)

Model的FileField有以下属性:

1
2
3
4
5
6
7
8
9
>>> car = Car.objects.get(name="57 Chevy")
>>> car.photo
<ImageFieldFile: chevy.jpg>
>>> car.photo.name
u‘cars/chevy.jpg‘
>>> car.photo.path
u‘/media/cars/chevy.jpg‘
>>> car.photo.url
u‘http://media.example.com/cars/chevy.jpg‘

Model的FileField是一个File对象,除了具有File对象的各种方法外,还有一个额外的save()方法:

FieldFile.save(name, content, save=True)

 name为存储名字,content为File或者File子类的实例

1
2
>>> car.photo.save(‘myphoto.jpg‘, content, save=False)
>>> car.save()

类似于

1
>>> car.photo.save(‘myphoto.jpg‘, content, save=True)

手动存储:

1
2
3
4
5
6
from django.core.files.baseimportContentFile
photo=request.FILES.get(‘photo‘,‘‘)
ifphoto:
file_content = ContentFile(photo.read())#创建File对象
car.photo.save(photo.name, file_content)#保存文件到car的photo域
car.save()

Django处理文件上传File Uploads

原文地址:https://www.cnblogs.com/guoyunlong666/p/9048754.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved