开发者

Django Multiple File Upload

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开发者_如何学Python 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


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


I haven't done this exact thing before, but it seems like you'd need to do some processing on the actual audio file before saving it.

The general structure would be:

if form.is_valid():
    object = form.save(commit=False)
    t = handle_uploaded_file(request.FILES['file'])
    object.field.save(t[0], t[1])

And in the handle_uploaded_file, you'd probably need to use something like ffmpeg to process the audio and then return (filename, content) to your main function.

In addition, using .chunks would be on the actual file passed:

str=""
for c in request.FILES['file'].chunks(): 
   str += c


In addition to handling the file-array in the request-object correctly as pointed out in the other posts, you should also make sure that in the html input, you have a "multiple"-attribute that is set to true. Example:

<input name="file_field" multiple="true" required="false" id="id_file_field" data-enpass.usermodified="yes" type="file">

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜