问题描述
我正在寻找一种很好的方法,将Firefox中的canvas元素上传到网络服务器或数据库,以便稍后重新加载它。
I'm searching for a good method to upload a canvas element from Firefox to a webserver or database to have the ability to reload it later.
我的想法:
1.我的第一个想法是使用getImageData()并将canvas作为ImageData对象保存到数据库,但这可能不是一个好的解决方案,因为这些对象可能会变得相当大。
2.第二个想法是使用Flash / Javascript方法将画布作为PNG上传到网络服务器。
My ideas:1. my first idea was to use getImageData() and save the canvas as an ImageData object to the database, but this might not a good solution because these objets can get quite large.2. second idea is to use a Flash/Javascript method to upload the canvas as an PNG to the webserver.
您对这些方法有任何意见
Do you have any comments on these methods or maybe have another good solution?
推荐答案
Canvas元素有一个 toDataURL
函数,用于将画布上的图像序列化为PNG,并编码为。您可以使用表单(通过设置隐藏的输入元素的值到数据网址)或使用AJAX在后台发布图片。
Canvas elements have a toDataURL
function that serializes the image on the canvas as a PNG, encoded into a data URL. You could either post the image with a form (by setting a hidden input element's value to the data URL) or in the background using AJAX.
您应该注意 toDataURL
(或任何其他获取像素数据的方法)将抛出一个安全异常,如果画布不是原始清洁。例如,如果您使用来自其他域的图像调用 drawImage
,您将无法再使用 toDataURL
或 getImageData
函数。
You should be aware that toDataURL
(or any other method of getting the pixel data) will throw a security exception if the canvas is not "origin-clean". For example, if you ever call drawImage
with an image from a different domain, you can no longer use the toDataURL
or getImageData
functions.
这篇关于将画布元素上传到webserver /数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!