我正在通过 ajax 上传文件,然后创建一个 SimpleUploadFile 对象并将其传递给replace request.FILES——这不会传递form.is_valid()。我在下面记录了 SimpleUploadedFile 字典和 request.FILES 替换;如您所见,可以创建 SimpleUploadedFile 对象:
SimpleUploadedFile [This does not work.]:
{'book_pics': <SimpleUploadedFile: movies.jpg (text/plain)>} # Yielded by printing uploaded_file
当我不使用 ajax 并简单地使用表单提交文件时[出于好奇我这样做了],这里是 upload_file 的样子:
request.FILE generated InMemoryUploadedFile [This works.]:
<MultiValueDict: {u'book_pics': [<InMemoryUploadedFile: movies.jpg (image/jpeg)>]}>
知道如何让 SimpleUploadedFile 进行验证吗?
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:
...
根据这些文档,SimpleUploadedFile 应该可以工作:https ://docs.djangoproject.com/en/dev/ref/forms/api/
编辑:
当我打印 f.clean(my_file) 时,出现以下错误,上下文如下。无论是否在 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...