Django多文件上传 [英] Django Multiple File Upload

查看:443
本文介绍了Django多文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单包含以下标题:

 < form enctype =multipart / form-data =invisibleaction =/ calendar / createEvent /method =POST> 

和以下正文:

 < input class =multiFileInputtype =filename =filesonchange =newInput();> 
< input class =multiFileInputtype =filename =filesonchange =newInput()>
< input class =multiFileInputtype =filename =filesonchange =newInput()>

随着很多其他输入,但文件上传是重要的。



此表单提交到我的视图,除了上传文件之外,还有一切正常。



当我,在视图中,执行print request.FILES我得到:

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

但是当我尝试用更多的东西,不会让我把它们用作文件。 / p>

例如,说我有以下表:

 类文件(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')

如果在我看来,我说:

  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得到以下异常:

  DjangoUnicodeDecodeError:'utf8'编解码器无法解码位置0中的字节0xff:无效的起始字节。您通过'\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'

此外,如果我尝试使用f.chunks()我得到

  AttributeError:'str'对象没有属性'chunks'
/ pre>

任何一种帮助将不胜感激。我已经停留了一段时间,并且会喜欢一些帮助

解决方案

您应该使用getlist访问multipart值,即:

  for request.FILES.getlist('files'):
文件(file = afile,files =测试).save()

我不认为这是列表作为一个python列表,当你使用 request.FILES ['files']



另外,如果要使用HTML5多文件上传而不是许多文件格式,请看这里:具有多个文件的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

解决方案

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

这篇关于Django多文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆