Django handles file uploads of instances of File Uploads

  • 2020-10-07 18:45:51
  • OfStack

HttpRequest.FILES

The file object uploaded by the form is stored in the class dictionary object request.FILES, and the form format needs to be multipart/ form-ES8en


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

The keys in ES12en. FILES come from the form < input type="file" name="" / > The name value:


img=request.FILES['image']

The values in request.FILES are UploadedFile class file objects.

UploadedFile

UploadedFile is a class file object with the following methods and properties:


UploadedFile.read()

Read the data of the entire uploaded file. Use caution when the file is large.


UploadedFile.multiple_chunks(chunk_size=None)

Determine if the file is large enough, 1 generally 2.5M


UploadedFile.chunks(chunk_size=None)

Returns a generator object, which should be used instead of read() when multiple_chunks() is True.


UploadedFile.name

Upload file to name.


UploadedFile.size

The size of the uploaded file.


UploadedFile.content_type

content_type header when the file is uploaded, for example (e.g.text /plain or application/pdf).


UpladedFile.charset

coding

Storing files

When you want to store the uploaded file locally:


f=request.FILES['image']
with open('some/file/name.txt', 'wb+') as destination:
 for chunk in f.chunks():
  destination.write(chunk)

Use Form to process uploaded files

You can also use form, which comes with django, to handle uploaded files.

First create Form with FileFiled or ImageFiled:


img=request.FILES['image']
0

Using Form:


img=request.FILES['image']
1

view function:


img=request.FILES['image']
2

Use Model to process uploaded files

If you create an Model with FileField or ImageField domains, you need to store the uploaded file to the FileFIeld domain of Model.

For example, when using the nicEdit text editor, you need to store the uploaded files and create Model:


from django.db import models
 
class NicEditImage(models.Model):
 image = models.ImageField(upload_to='nicedit/%Y/%m/%d')

Create ModelForm:


img=request.FILES['image']
4

view:


img=request.FILES['image']
5

You can also manually store files into Model's file domain:


img=request.FILES['image']
6

Form processing is not used

If you want more freedom, you can do it all by hand.


img=request.FILES['image']
7

FileField of Model has the following attributes:


>>> 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'

The FileField of Model is an File object, and in addition to the various methods that have an File object, there is an additional save() method:


img=request.FILES['image']
9

name is the storage name, and content is an instance of File or File subclass


>>> car.photo.save('myphoto.jpg', content, save=False)
>>> car.save()

Similar to the


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

Manual storage:


from django.core.files.base import ContentFile
photo=request.FILES.get('photo','')
if photo: 
 file_content = ContentFile(photo.read()) # create File object 
 car.photo.save(photo.name, file_content) # Save file to car the photo The domain 
 car.save()

Related articles: