I am using the following code to use a device camera to capture an image, a still photo and show it in the browser. It is working fine but what I would like to do is store the image with a unique number into a website folder and store a link to it in a database. So my question is how do I grab the image data with js and store it in a folder with a reference number. Would be very grateful for any help.
<html>
<body>
<video id="video" width="640" height="480" autoplay></video>
<button id="snap">Take Photo</button>
<canvas id="canvas" width="640" height="480"></canvas>
<script>
let canvas = document.querySelector("#canvas");
let context = canvas.getContext("2d");
let video = document.querySelector("#video");
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia){
navigator.mediaDevices.getUserMedia({video: true}).then((stream) =>{
video.srcObject = stream;
video.play();
});
}
document.getElementById("snap").addEventListener("click", () => {
context.drawImage(video, 0, 0, 640, 480);
});
</script>
</body>
</html>
I have found following code but needs manual click to download and selcet a folder'
function downloadCanvas(){
// get canvas data
var image = canvas.toDataURL();
// create temporary link
var tmpLink = document.createElement( 'a' );
tmpLink.download = 'image.png'; // set the name of the download file
tmpLink.href = image;
// temporarily add link to body and initiate the download
document.body.appendChild( tmpLink );
tmpLink.click();
document.body.removeChild( tmpLink );
}
<button onclick="downloadCanvas()">Download Phot!</button>
I really wanted it to automatically save the image to the site folder for say photos.