I'm in the developmental stage of my custom cup eCommerce site. I am working on a personalization feature so my customers can design their own cup. Everything is about finished. I can preview, print, and export the canvas image, as seen here: https://metaldepot.americommerce.com/Designer/index.html When clicking the export button, the image can be generated and downloaded to the users computer . However, I'd like the canvas image to be generated and uploaded to my website server . Here is the coding I have so far for the export button in my index.html file and my app.js file
app.js:
// export as DESIGN
$('.export').click(function(){
//hide options
$('#printable').find('i').css('display', 'none');
$('#printable').find('.ui-icon').css('display', 'none');
//get printable section
var exportCanvas = document.getElementById('printable');
//get convas container
var canvasContainer = document.getElementById('convascontent');
//export canvas to convascontainer
html2canvas(exportCanvas, {
//when finished fucntion
onrendered: function(canvas) {
// initialize canvas container (if we generate another canvas)
$('#convascontent').html(' ');
// append canvas to container
canvasContainer.appendChild(canvas);
//add id attribute to the canvas
$('#convascontent').find('canvas').attr('id','mycanvas');
// display options again
$('#printable').find('i').css('display', 'block');
$('#printable').find('.ui-icon').css('display', 'block');
//document.getElementsByTagName("UL")
}
});
// return false;
});
//export options
$('.exportas').click(function(){
// get type to export
var to = $(this).data('type');
// alert(to);
// get our canvas
var oCanvas = document.getElementById("mycanvas");
// support variable
var bRes = false;
if(to == 'png'){
// export to png
bRes = Canvas2Image.saveAsPNG(oCanvas);
}
if(to == 'jpg'){
// maybe in some old browsers it works only on Firefox
bRes = Canvas2Image.saveAsJPEG(oCanvas);
}if(to == 'bmp'){
Res = Canvas2Image.saveAsBMP(oCanvas);
}
// if browser doesn't support mimetype alert user
if (!bRes) {
alert("Sorry, this browser is not capable of saving " + strType + " files!");
return false;
}
});
index.html
<!-- export option (png, jpg, bmp) -->
<li>
<div class="btn-group dropup">
<a class="dropdown-toggle export btn" data-toggle="dropdown" href="#">
Export
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li>
<a href="#" class="exportas" data-type='png'>PNG</a>
<a href="#" class="exportas" data-type='jpg'>JPG</a>
<a href="#" class="exportas" data-type='bmp'>BMP</a>
Any help would be greatly appreciated, thank you for your time!
You can get contents, and send via ajax to server.
$('.save').click(function(){
// get our canvas
var oCanvas = document.getElementById("mycanvas");
var data = oCanvas.toDataURL(); // default png
// var data = oCanvas.toDataURL('image/jpeg'); // for jpg
$.post('/url_to_upload', { customer : 'id', data : data });
});
now, server side - posted vars contains data like
"data:image/png;base64,/9j/4AAQS....."
split with first comma ',' , and decode base64. then save to DB or FS or.. anywhere you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.