Volevo caricare i file immagine, disegnarli su tela, apportare modifiche e salvarlo nel database. Ho provato a testare il valore base64 che l'immagine canvas (Pic
) ha restituito ed è vuota. Tuttavia, vedo il risultato quando aggiungo la tela (Pic
) al documento. Cosa sto facendo di sbagliato qui?HTML5 Canvas toDataURL restituisce vuoto
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
for (var i = 0, f; f = files[i]; i++) {
if (!f.type.match('image.*')) {
continue;
}
// read contents of files asynchronously
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
var canvas = document.createElement("canvas");
var datauri = event.target.result,
ctx = canvas.getContext("2d"),
img = new Image();
img.onload = function() {
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
};
img.src = datauri;
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
document.body.appendChild(canvas); //picture gets uploaded
// Generate the image data
var Pic = canvas.toDataURL("image/png");
console.log(Pic); // => returns base64 value which when tested equivalent to blank
Pic = Pic.replace(/^data:image\/(png|jpg);base64,/, "")
// Sending image to Server
$.ajax({
// …
});
};
})(f);
reader.readAsDataURL(f);
}
}
Perché il parametro oggetto della chiamata '$ .ajax' (che è' // Invio dell'immagine al server') è vuoto? Aggiungi un commento se hai saltato intenzionalmente alcune righe. –
'console.log (Pic);' restituisce qualcosa? – hindmost
sì, fornisce qualche valore di base 64 che ho testato fondamentalmente vuoto: – user3399326