Am doing a project in angular js and django.
This is the model.
class MessageAttachments(models.Model):
filename = models.CharField(max_length=100)
file_size = models.IntegerField(max_length=100)
attachment = models.FileField(upload_to='files/')
def __unicode__(self):
return self.filename
Inside api.py i wrote my own function to save message attachments.
def send(self, request, **kwargs):
self.method_check(request, allowed=['post'])
data = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json'))
file_data = data.get('attach', '')
try:
b = file_data.encode('utf-8')
message_attach = MessageAttachments()
message_attach.filename = 'file'
message_attach.file_size = 'file'
message_attach.attachment = ContentFile(b64decode(b), 'test')
message_attach.save()
return self.create_response(request, {
'success': True,
'msg': msg.id
})
except:
return self.create_response(request, {
'success': False
})
As you can see i tried ContentFile . But its not working. I need to get the file size too from that. The string is stored inside variable file_data. i tried b64decode from base64. Even though the decoding works file is not stored.
Am using python 3.4 and django 1.6.