Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Multiple File Upload

Tags:

django

I have a form that has the following header:

<form enctype="multipart/form-data" target="invisible" action="/calendar/createEvent/" method="POST">

and the follow body:

<input class="multiFileInput" type="file" name="files" onchange="newInput();">
<input class="multiFileInput" type="file" name="files" onchange="newInput()">
<input class="multiFileInput" type="file" name="files" onchange="newInput()">

Along with a lot of other inputs but the file upload are the important one.

This form gets submitted to my view and does everything correctly except for the file uploading.

When I, in the view, execute "print request.FILES" i get:

<MultiValueDict: {u'files': [<TemporaryUploadedFile: boson.mp3 (audio/mpeg)>, <TemporaryUploadedFile: hadron.mp3 (audio/mpeg)>]}>

But when I try to do more with those it won't let me use them as files.

For example, say I have the following tables:

class File(models.Model):
    file = models.FileField(upload_to='files')

class Test(models.Model):
    name = models.CharField(max_length=10)
    files = models.ManyToManyField(File, related_name='files')

If in my view i say:

for f in request.FILES['files']:

    test = Test()
    test.name='test'
    test.save

    empt = File()
    empt.file = f
    empt.save()

    test.files.add(empt)

I get the the follow exception:

DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte. You passed in '\xff\xfb\xe0d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Info\x00\x00\x00\x0f\x00\x00\x98C\x02m~\t\x00\x03\x05\x08\n'

Also, if I try to write to a destination say using f.chunks(), I get

AttributeError: 'str' object has no attribute 'chunks'

Any sort of help would be greatly appreciated. I've been stuck on this for a while and would love some help

like image 202
aaivazis Avatar asked Aug 25 '11 00:08

aaivazis


People also ask

What is FileField in Django?

FileField is a file-upload field. Before uploading files, one needs to specify a lot of settings so that file is securely saved and can be retrieved in a convenient manner. The default form widget for this field is a ClearableFileInput.

What is SimpleUploadedFile?

class SimpleUploadedFile(InMemoryUploadedFile): """ A simple representation of a file, which just has content, size, and a name. """ def __init__(self, name, content, content_type="text/plain"): content = content or b"" super().


1 Answers

You should access multipart values with getlist, i.e.:

for afile in request.FILES.getlist('files'):
    File(file=afile, files=test).save()

I don't think it's getting the list as a python list when you use request.FILES['files'].

Also, if you want to use the HTML5 multiple file upload instead of many file forms, take a look here: django form with multiple file fields

like image 55
Edd Avatar answered Sep 24 '22 19:09

Edd