Django FileField not validating with a SimpleUploa

2019-04-12 17:04发布

I am uploading a file via ajax, and then creating a SimpleUploadFile object and passing it to replace request.FILES -- this does not pass form.is_valid(). I've logged the SimpleUploadedFile dictionary and request.FILES replacement below; as you can see, the SimpleUploadedFile object is able to be created:

SimpleUploadedFile [This does not work.]:
{'book_pics': <SimpleUploadedFile: movies.jpg (text/plain)>} # Yielded by printing uploaded_file

When I don't use ajax and simply use the form to submit the file [I did this out of curiousity], here is what uploaded_file looks like:

request.FILE generated InMemoryUploadedFile [This works.]:
<MultiValueDict: {u'book_pics': [<InMemoryUploadedFile: movies.jpg (image/jpeg)>]}>

Any idea of how I could get the SimpleUploadedFile to validate?

def add_book(request):
    if request.method == 'POST':
        # Grabbing the file uploaded via ajax earlier.
        fh = File(file(settings.MEDIA_ROOT + request.POST['book_pics']))
        uploaded_file = {'book_pics':SimpleUploadedFile(fh.name, fh.read())}
        form = BookForm(data=request.POST, files=uploaded_file)
        if form.is_valid():
            print >> sys.stderr, "Form is valid."
            form.save()
            return render(request, 'generic_message.html', {'message': 'Succesfully added book.'})
        else:
           ...
    else:
        ...

According to these docs, SimpleUploadedFile should work: https://docs.djangoproject.com/en/dev/ref/forms/api/

EDIT:

When I print f.clean(my_file) I get the following error, context below. This happens with and without setting filetype in my_file:

fh = File(file(settings.MEDIA_ROOT + request.POST['book_pics']))
my_file = UploadedFile(fh.name, fh.read(), 'image/jpeg')
f = forms.FileField()
print >> sys.stderr, f.clean(my_file)
# f.clean(my_file) Returns an error: 

DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in position 18: invalid 
start byte. You passed in '<UploadedFile: 
X\x06\x18\x92HI\xde\xf0\x00\xdd\x7f\n\x8e\xf9\x8f\xe5\x00\x92IGa\xaa\xce\xfe_\xa4\x04\xe
3\xca\x1a,\x97\x99\xd0\xda\x02\xe3e\xa8\xbb=\x99\xe0\x00\x0b\xfd?
Q\x00\x02\x80\x00*\xe6\xf7\xb0\xd6\xfb@\x06\xd2\x1f7\xfd\xaf\x00\x04Iw}\x8b\x08\xbcxB\xc
bj\x1e\xa3\x7f1\xf9\xc3\xc3\xef"r\xfb\xcc"T\xa2\x01r\xe7X\xb0\xa3\xc4r\xea\xdf\x9c\x00>\
x96K\x0b\xee\x07\xd26<\xa0\r\xb8\xf2?\xa4\\\x94\xf96\xe43\x1f\xc5\xfa\x1f\xd8@ 
t\xe9\xea\x7f8\x00p?S\xf9\xc0\x01\xc6\xaa\x1b\x06a\xca-
\x1f\xba\x88\xa6\xedn,|\'\xa2\xd8t\xb0\x862\\\xb0\xfa\x17\x16<\xf7\x80\xc1\xce\xc3\xcc\x
fe\x91Xp\x02\xc5\x92\x96\xb3\xa9\x8bo\xac8\x0eQ\xa1\xf3\x80\x07(\xf8GR\xf1x\xf0\x80($\xa
8\x02K76\xda4\x03h\x07\x9f\xed\x00\x07\x1f\x12F\xc7\xfbC\xc3\x90\x16\t\xca\xeeu;\xf4\x8a
\x92\x9f#\xa4\x12Ar\xf7\x80A\xcaId\xdfR\xc7\xae\xb0\xf0\x01i%\xc5\xf7\x8a\x80PI\t\xb9\xb
9 \xfd`\x01\xc2I\xe6}\x81\x1b\x7f*7\x8a\x1c\'O|\x84\\\xc1\xc...

1条回答
霸刀☆藐视天下
2楼-- · 2019-04-12 17:50

From one of my tests:

name = 'filename'
f = file(self.TEST_ROOT + name)
file_data = {'file':SimpleUploadedFile(f.name,f.read())}
data = {}
form = self.TestForm(data,file_data)
self.assertTrue(form.is_valid())

This test is passing. My code is almost identical to yours, except i'm not passing a File but a file to the form. And i do not name the arguments passed. Both differences don't make a difference, i have tried that. Your problem is that in

file(settings.MEDIA_ROOT + request.POST['book_pics'])

request.POST['book_pics'] does contain the filename, but your file is not in MEDIA_ROOT. It is in the location where django puts temporary uploaded files, or, if it is a small file, it is in memory. Either way the path you create with your code does not point to the file.

If you try to send the file via ajax, and not with a form, you can do that with a solution like this one: http://kuhlit.blogspot.de/2011/04/ajax-file-uploads-and-csrf-in-django-13.html Have a look at the django code somewhere in the middle of that page.

Quite complex, but it works.

查看更多
登录 后发表回答