我正在尝试将图像android(客户端)通过转换为base64字符串发送到django(服务器)。我收到字符串@服务器端,解码并重命名。但是,当我尝试保存图像时,它失败了。
这是图像应存储在服务器上的路径。 models.py
class App(models.Model):
name = models.CharField(max_length=200)
#create_date = models.DateTimeField('date created')
category = models.CharField(max_length=200, null=True)
app_logo = models.ImageField(upload_to="/root/AR_BROWSER/example/static/uploaded_pics", null=True, blank=True)
user = models.ForeignKey(App_User)
def __unicode__(self):
return self.name
views.py中的函数
@csrf_exempt
def create_app(request):
appName = request.POST['name']
user = request.POST['userID']
c = request.POST['category']
i = request.POST['image']
imgdata = base64.b64decode(i)
t = datetime.now()
filename = t.strftime('%Y%m%d%H%M%S.jpg')
with open(filename, 'wb') as f:
f.write(imgdata)
u=App_User.objects.get(id=user)
apps = App.objects.create(name = appName, category=c, user_id = u.id, app_logo=filename)
apps.save()
return HttpResponse("You created %s." % apps.name)
答案 0 :(得分:0)
你可以使用GridFS: 通过这个链接: