Save and load image from client to server with django

我的梦境 提交于 2020-01-06 10:54:57

问题


Here is how I send data from the client (coffeescript & dajaxice):

imageData = canvas.toDataURL("image/png")
Dajaxice.draw.saveImage( @saveImage_callback, {'image': imageData } )

Here is how I save my image on the server (taken from this answer)

@dajaxice_register
def saveImage(request, image):

   imageData = re.search(r'base64,(.*)', image).group(1)
   output = open('image.png', 'wb')
   output.write(imageData.decode('base64'))
   output.close()

I would like to load the image and send it like so:

inputfile = open('image.png', 'rb')
imageData = inputfile.read().encode('base64')
inputfile.close()
return simplejson.dumps( { 'image': imageData } )

But this does not give me the exact same data, and my client fails to draw the returned image. imageData ends with 2OWn9u2 when I write it, and 2OWn when I read it (missing '9u2').


回答1:


Ok, the difference of data is not a problem, it works. Here is how I draw the returned image on my client:

saveImage_callback: (result)=>
    imageData = 'data:image/png;base64,'+result.image
    image = new Image()
    image.src = imageData
    canvas.getContext("2d").drawImage(image, 300, 300, 300, 300)


来源:https://stackoverflow.com/questions/24449101/save-and-load-image-from-client-to-server-with-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!