Processing image upload form in django: when to use save () vs chunks () vs cleaned_data?

I successfully uploaded the image using the following code:

views.py

from django.conf.urls.defaults import * from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render_to_response from django import template from django.template import RequestContext from mysite.uploadr.forms import UploadFileForm def upload_file(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): form.handle_uploaded_file(request.FILES['file']) return HttpResponse(template.Template(''' <html><head><title>Uploaded</title></head> <body> <h1>Uploaded</h1> </body></html> ''' ).render( template.Context({})) ) else: form = UploadFileForm() return render_to_response('upload.html', {'form': form}, context_instance=RequestContext(request)) 

forms.py

 from django import forms from settings import MEDIA_ROOT class UploadFileForm(forms.Form): title = forms.CharField(max_length = 50) file = forms.FileField() def handle_uploaded_file(self,file): #print type(file), "file.name=",file.name #print dir(file) destination = open(MEDIA_ROOT + '/images/'+file.name, 'wb+') for chunk in file.chunks(): destination.write(chunk) 

I would like to take one more step and associate the image with the user who is loading. I saw a few examples and fell in love with the technique in this post: https://stackoverflow.com/a/318969/

I noticed that their code uses save () and cleaned_data. Isnโ€™t it necessary to iterate over the pieces and write to the destination folder, as in the examples in the documentation? Do i need to use cleaned_data? Just trying to figure out the most efficient ways to upload files, I saw so many different ways to do this. Your help is greatly appreciated.

+4
source share
2 answers

Optional if the file is larger than settings.FILE_UPLOAD_MAX_MEMORY_SIZE (default 2.5M in django 1.2)

Take a look at the django.core.files.storage.FileSystemStorage class. The save () method performs the task of saving pieces for you and makes the file lock correctly.

 storage = FileSystemStorage( location = '/var/www/site/upfiles', base_url = '/upfiles' ) content = request.FILES['the_file'] name = storage.save(None, content) #you can use some suggested name instead of #None. content.name will be used with None url = storage.url(name) #<-- get url for the saved file 

In older versions of django (for example, in version 1.0), an error occurred while generating file names. He continued to add _ to the file names, and the name of the downloaded file increased longer if you re-upload the same file. This seems to be fixed in version 1.2.

+4
source

When directly accessing request.FILES['file'] you bypass any processing that your UploadFileForm performs (you donโ€™t even need a form class to process such files). form.cleaned_data['file'] will access the processed (and cleared if you added a clean method) form data. You can also access the request.POST dictionary directly, not the form data. If you have no good reason, it is better to use data of a refined form.

In the example you indicated, the model was also used (which was called by the save () method), and it was the model field that handled access to the file. If you want to store information about your downloaded files in a database, this is the way to go.

You can use the built-in file storage API to save files: http://docs.djangoproject.com/en/dev/ref/files/storage/#ref-files-storage .

In addition, you should not just call open(MEDIA_ROOT + '/images/'+file.name, 'wb+') with the user specified file name. It just asks for directory traversal or other problems.

+1
source

All Articles