I have created image using svg and javascript I want user to be able to save it. How do I send this to bean and save it?
view code:
<script>
var svg = $('#map').html().replace(/>\s+/g, ">").replace(/\s+</g, "<");
// strips off all spaces between tags
canvg('cvs', svg, {
ignoreMouse: true,
ignoreAnimation: true
});
var canvas = document.getElementById('cvs');
var img = canvas.toDataURL("image/png");
</script>
<h:body>
<center><div id="map"></div></center>
<a href="#" id="saveBtn">SAVE</a>
<canvas id="cvs" width="0" height="0" ></canvas>
<h:form id="formId">
<h:inputText id="inputId" value="#{bean.property}" />
<h:commandButton id="buttonId" value="Button" action="#{bean.action}" />
</h:form>
</h:body>
You can send the dataURL to the backing bean via an hidden input field like this:
and set the value of the inputHidden value from your javascript like this:
UPDATE: The toDataURL method returns a string and you can save the same as string in the backing bean, the string is base64 encoded so it can be decoded back into an image and saved to disk at the root of the webapp: