Pass Canvas Image URL to another JSP - html5-canvas

I am creating a Hand written image Digit recognizer. I have created an HTML canvas for users to draw a digit. When the user clicks on Predict button, the variable which stores canvas.toDataURL should be send to the server.
HTML code which displays 2 buttons, one for Save and one for Clear shown below:
<button type="button" id="save" class="btn btn-default">Predict</button>
<button type="button" id="clear" class="btn btn-default">Clear</button>
I have created a javascript for the id=save as:
var saveButton = document.getElementById("save");
saveButton.addEventListener('click', saveImage);
function saveImage(){
var data = canvas.toDataURL("image/png");
}
I am unable to pass the variable 'data' to server. I can see the base64 encoded png format when I do a window.open(data, '_blank', 'location=0, menubar=0');. But thats not want I want. Any help would really be appreciated. Thanks.

Related

Laravel Livewire/AlpineJS: Disable a button while loading data from an API and then enabling it again

This should be really simple, but I don't get it. I want to replicate the functionality of a button that when pressed goes to an API (which could take about a minute to get the data and process it), it gets diabled, and after loading the data it gets enabled.
I'm using Laravel/Livewire/Alpine
So, in my livewire component I have:
public $loading = false;
In my blade file, I have the declaration for the div where the button is:
<div
class="grid grid-cols-3 gap-4"
x-data="{
loading: #entangle('loading')
}"
>
Then the button x-binds the disabled property to the loading value, when the button is clicked, it changes the property of the loading variable, and calls the loader function
<button
type="button"
class="btn btn-sm btn-jbn"
x-on:click="loading = true"
x-bind:disabled="loading"
wire:click="loader"
>
Load API
</button>
And it does what it is supposed to do... the button is grayed, it becomes unusable, the cursor change, etc., it executes the loader function in my livewire component, but it never return to the normal state after loading the API. In my livewiere componente I have:
public function loader() {
// API call and logic goes here, this works
$this->loading = false;
}
So I would imagine that at the end of the API process the entangled variable loading would return the button to its normal state, but it doesn't
What am I missing?
Livewire already has incorporated functionality to handle loading-states. Instead of implementing your own, you can use this.
Get rid of all your current loading-logic, and simply use wire:loading with wire:target on your button.
wire:loading can toggle the disabled attribute directly by doing wire:loading.attr="disabled", and wire:target is to set the target for that loading-state to the method you are calling, so in your case that's wire:target="loader".
This means your button looks like this,
<button
type="button"
class="btn btn-sm btn-jbn"
wire:click="loader"
wire:loading.attr="disabled"
wire:target="loader"
>
Load API
</button>

Upload file to S3 using Ajax and Boto3 for Django

I have a django project in which I would like to upload all images to S3. I have installed boto3 and am able to print out my buckets. Thus I know I have a definite connection. Now my next step is to push a file to S3 using ajax. Below is the current setup I have. Will this approach work? One problem I have stumbled upon is the fact that Chrome returns C:\fakepath\filename.
Django view:
def change_project_image(request):
image = request.POST['image']
data = open(image, 'rb')
s3.Bucket('bucket-name').put_object(Key='filename.jpg', Body=data)
return HttpResponse('temporary return')
Javascript:
$('#project-image-upload').on('change', function(){
var image = $(this).val();
$.post('/project/change-project-image/', {image:image} function(data){
}, 'json');
});
HTML:
<label class="btn btn-default btn-file">
Browse <input id="project-image-upload" type="file">
</label>
So I took a different approach and was able to access the object in the view. I added the input to a form which posts to my desired view and then I handle the file like so:
def change_project_image(request):
image = request.FILES['image']
#now I need to pull the temporary path of image and pass it to boto
data = open('path_to_file', 'rb')
s3.Bucket('bucket_name').put_object(Key='filename.jpg', Body=data)
The problem I had was that I forgot to add enctype="multipart/form-data" in my form so request.FILES['image'] was empty.

Bootstrap Dynamic Modals With AJAX Calling

I'm using codeigniter v3 and bootstrap v3.
I've a table for messages and for every message in table, when click on details button, a bootstrap modal show the details.
In a for loop, I print table rows and for every row (at end of the loop), I put whole bootstrap modal structure.
My question is: how could do this with ajax calling? I mean, I don't put all modal code for every table row (every message) and every time that details button clicked, I handle showing modal with ajax.
thanks for attention.
SOLVED:
I find an easy and I think better way in the bootstrap official website: Using data- attribute.
I show it with an working example for who that could not achieve better solution:
Suppose we want to fill a table body with some data (as my problem):
//just table body code
<tbody>
<?php foreach($fields as $field): ?>
<tr>
<td><?php $field->some_field; ?></td>
//other <td> elements
<td>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal"
data-time="<?php echo $field->time; ?>">Show Details</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
So in a loop, I read data (that in controller, get with model's method, and pass to view with $data['fields']). Now for showing some details in modal, I use HTML5 data attribute. Suppose, I want to show time in the modal. As you see in above code, I put time field in data-time:
data-time="<?php echo $field->time; ?>"
Now I create a modal as template (you could see whole modal structure in bootstrap official website) and put it out of loop (one modal for all table rows; dynamic data). in the following code, I put an element in the modal body (as a placeholder):
//... in the modal body section
<h4 id="time"></h4>
This element have no content, because I want to retrieve every row time filed and put it in this element. Note this will be practical with defining an id. Now some script:
//first load jquery
<script type="text/javascript">
$(document).ready(function(){
$('#myModal').on('show.bs.modal', function(event){
var btn = $(event.relatedTarget); // find which button is clicked
var time = btn.data('time'); //get the time data attribute
$('#time').text(time); //put the data value in the element which set in the modal with an id
});
});
</script>
You could define more data attribute and retrieve in this way.
I will not write the code for you, but show you the way:
You just need one modal for the whole page and it works like a template.
You need same data/informations to be displayed in the modal. This data you can pick up via Javascript/jQuery from your HTML. I prefer data attributes for that. Something like:
data-modal-title="My Title" data-modal-content="My Content"
Now you inject this data in your modal template and open the modal via Javascript.

DOMPDF Laravel 5 Interactive Page

I'm using Laravel 5 to generate a form for a warehouse. In Main form you can select which items to get and it should generate a PDF with the items (all info), people to get the items, date and invoice number.
All the information is on a DIV called 'invoice'. How can I send this object to a new view to generates the PDF. I read about 'invoking dompdf via web' to make it more interactive but the documentation is not clear enough or doesn't fit with I'm needing.
This is my current code:
From:
Recibo.js
$('#createPDF').click(function()
{
$htmlData = "data";
$_token = $('[name="_token"]').val();
$.get('recibos/pdf',{ html:$htmlData, _token: $_token })
.done(function(data)
{
console.log('Done PDF!');
});
});
ReciboController.php
public function reciboPDF(Request $request)
{
$data = $request->get('html');
$pdf = \PDF::loadView('create.template.formReciboPDF',compact('data'))->setPaper('letter')->setOrientation('landscape');
return $pdf->stream();
//return view('create.template.formReciboPDF');
}
formReciboPDF.blade.php
<tr>
<td>{{$data}}</td>
<td>Papeleria</td>
<td>Unidad</td>
<td>Grande</td>
<td>La Palma</td>
<td>2342423424234</td>
<td>Bueno</td>
<td>9</td>
</tr>
Instead of trying to capture the HTML can you send the selection parameters to the server and rebuid the page? Then you would just need to feed the generated HTML into dompdf and render. If visual appearance is important you could use hidden checkboxes as the selection mechanism.
If you want to use a shortcut you can capture the HTML using jQuery.contents().
In your second part it looks like you're trying to send your request via AJAX and get back the PDF. While this is technically possible it's more trouble than it's worth. An easier alternative would be to submit a hidden form to a blank window. It would require some minor changes to your HTML/JS but little else, e.g.
$('#createPDF').click(
function() {
$('#html').val($('#invoice').content());
$(this).closest('form').submit();
});
<form method="POST" action="recibos/pdf" target="_blank">
<input type="hidden" name="html" id="html" value="" />
<input type="hidden" name="_token" value="{value from server}" />
<button type="button" id="createPDF">Create PDF</button>
</form>

How to pass the HTML ID and URL of image in a WP post?

I want to incorporate the Aviary editor into WordPress posts so that people can click an edit button for the image in the post, edit it and save it. Aviary has provided code for the last two, but I do not know how to pass the HTML ID and URL of the image in the post in an input tag for the edit button. I know this is basic, but I am not a programmer, so I would sure appreciate your patience and feedback. Thanks.
Here is the input static code Aviary provides:
<!-- Add an edit button, passing the HTML id of the image and the public URL of the image --><p>
<input type='image' src='http://images.aviary.com/images/edit-photo.png' value='Edit photo'
onclick="return launchEditor('imageid', 'url');" /></p>
I need to know how to pass the ID and URL for the last line of the code. Thanks so much for any feedback you can offer.
the imageid is the div id to use by aviary, you just need tu create
<div id="imageid"></div>
An to get the image url you can to like this
<?php
$image = wp_get_attachment_image_src($post->ID);
$url = $image[0];
?>
<input type='image' src='http://images.aviary.com/images/edit-photo.png' value='Edit photo' onclick="return launchEditor('imageid', '<?php echo $url; ?>');" /></p>
I assume that you will place it in attachment page

Resources