Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  134] [ 7]  / answers: 1 / hits: 35458  / 6 Years ago, sun, october 14, 2018, 12:00:00

I've created an image using canvas. I want to append the exact image file in the form data and not the url. This is my code.



<video></video>

<button type=button onclick=turnOn()>turn on cam</button>
<button type=button onclick=takeSnapshot()>capture image</button>


<script>
function turnOn() {
document.getElementsByTagName('video')[0].play();

var video = document.querySelector('video')
, canvas;

if (navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia({video: true})
.then(function(stream) {
video.src = window.URL.createObjectURL(stream);
})
.catch(function(error) {
document.body.textContent = 'Could not access the camera. Error: ' + error.name + + error.message;
});
}
}

function takeSnapshot() {
var video = document.querySelector('video')
, canvas;

var img = document.querySelector('img') || document.createElement('img');
var context;
var width = video.offsetWidth
, height = video.offsetHeight;

canvas = canvas || document.createElement('canvas');
canvas.width = width;
canvas.height = height;

context = canvas.getContext('2d');
context.drawImage(video, 0, 0, width, height);

img.src = canvas.toDataURL('image/png');
document.body.appendChild(img);

var fd = new FormData(document.forms[0]);
fd.append(image, img);

$.ajax({
type: POST,
enctype: 'multipart/form-data',
url: /api/file/upload,
data: fd,
processData: false,
contentType: false,
cache: false,
success: (data) => {
alert(yes);
},
error: function(xhr, status, error) {
alert(xhr.responseText);
}
});
}

</script>


It's appending the blob file ����... And I want it to append the exact image file. I haven't used this before and can't seem to understand other tutorials. Hoping you could help me. Thank you so much!!!



Edit:
I already edited the code and tried to append the same image I'm appending on the body. But it's not working..


More From » canvas

 Answers
8

You might try converting the based 64 encoded inline image returned by canvas.toDataURL('image/png') to the same kind of File object you get from an <input id=file type=file></input> element when using document.getElementById(file).files[0] and append that to fd.



To give that a try, change



var fd = new FormData(document.forms[0]);
fd.append(image, img);

$.ajax({
type: POST,
enctype: 'multipart/form-data',
url: /api/file/upload,
data: fd,
processData: false,
contentType: false,
cache: false,
success: (data) = > {
alert(yes);
},
error: function(xhr, status, error) {
alert(xhr.responseText);
}
});


to



fetch(img.src)
.then(res => res.blob())
.then(blob => {
const file = new File([blob], capture.png, {
type: 'image/png'
});
var fd = new FormData();
fd.append(image, file);
$.ajax({
type: POST,
enctype: 'multipart/form-data',
url: /api/file/upload,
data: fd,
processData: false,
contentType: false,
cache: false,
success: (data) => {
alert(yes);
},
error: function(xhr, status, error) {
alert(xhr.responseText);
}
});
});

[#53327] Monday, October 8, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sophiak

Total Points: 242
Total Questions: 90
Total Answers: 103

Location: Liechtenstein
Member since Wed, Dec 8, 2021
2 Years ago
;