I am trying to make an app where my users can draw pictures and then save them. And just like this person was doing I am trying to add a form using Carrierwave to upload.
This is the form that I am trying to upload to. I made the file field invisible to the user can only add images to the form that they draw.
<%= form_for(listing, html: { multipart: true } ) do |f| %>
<div class="invisible">
<%= f.label :image %>
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
I would like to use the following javascript function that I got from this website to convert the canvas image into a dataurl and then into a blob and then add the image to my form when the user clicks the submit button in my form.
function uploadimage() {
var dataURL = canvas.toDataURL('image/png');
// Convert dataURL to Blob object
function dataURLtoBlob(dataURL) {
// Decode the dataURL
var binary = atob(dataURL.split(',')[1]);
// Create 8-bit unsigned array
var array = [];
for(var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
// Return our Blob object
return new Blob([new Uint8Array(array)], {type: 'image/png'});
}
// Get our file
var file= dataURLtoBlob(dataURL);
// Create new form data
var fd = new FormData();
// Append our Canvas image file to the form data
fd.append("image", image);
// And send it
}
Another question that I have is say for example I just wanted to convert the canvas image into a url and then upload it to carrierwave as a base64 image using the code from the first method of this site. Like this...
function uploadimage() {
var dataURL = canvas.toDataURL('image/png');
}
On Rails Carrierwave Base64 image upload a poster named mohamed-ibrahim said that you can use the carrierwave-base64 gem. Could somebody please show me how to use the carrierwave-base64 gem. I've already changed my mount_uploader to mount_base64_uploader but I am confused about what else he said to do.