javascript - save image with ajax in ruby on rails -
i dont know how save image in ajax ruby on rails, repo in github, github.com/luis77/editor22 please chek out, have been trying 2 days, dont know wrong. example, check view attachment/new here code in javascript ajax code:
<script> var canvas = document.createelement('canvas'); var context = canvas.getcontext('2d'); var centerx = canvas.width / 2; var centery = canvas.height / 2; var radius = 70; context.beginpath(); context.arc(centerx, centery, radius, 0, 2 * math.pi, false); context.fillstyle = 'green'; context.fill(); context.linewidth = 5; context.strokestyle = '#003300'; context.stroke(); document.body.appendchild(canvas) var dataurl = canvas.todataurl('image/png'); // convert dataurl blob object function dataurltoblob(dataurl) { // decode dataurl var binary = atob(dataurl.split(',')[1]); // create 8-bit unsigned array var array = []; for(var = 0; < binary.length; i++) { array.push(binary.charcodeat(i)); } // return our blob object return new blob([new uint8array(array)], {type: 'image/png'}); } // our file var file= dataurltoblob(dataurl); // create new form data var fd = new formdata(); // append our canvas image file form data fd.append("image", file); // , send $.ajax({ url: "/attachments/change2", type: "post", data: fd, processdata: false, contenttype: false, }); </script>
it's specified in routes:
post "attachments/change2"
and method in controller,
but won't save, need code, please help.
assuming javascript code correct, , post data making server correctly, use carrierwave or paperclip handle file uploads.
Comments
Post a Comment