问题
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