1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <image src="alpine.jpg">
<script>
const urlDest="test.php";
function handleProgress() {}
function handleComplete() {}
function uploadFile(nomInpFile, imgSrc)
{
var xhr = new XMLHttpRequest(),
formData = new FormData();
// formData.append(nomInpFile, imgSrc, "voiture.jpg"); //version 1
formData.append(nomInpFile, imgSrc); //version 2
xhr.open('POST', urlDest, true);
xhr.onload = function(){
//alert(nomInpFile+' téléchargé dans '+urlDest);
handleComplete(imgSrc.size);
};
xhr.onerror = function(){
alert(this.responseText);
handleComplete(imgSrc.size);
};
xhr.upload.onprogress = function(event){
handleProgress(event);
}
xhr.send(formData);
}
document.getElementsByTagName("img")[0].addEventListener("load",function tutu() {
this.removeEventListener("load",tutu);
const canvas=document.createElement("canvas");
canvas.setAttribute('width',this.width);
canvas.setAttribute('height',this.height);
canvas.getContext("2d").drawImage(this,0,0);
// canvas.toBlob(function(blob) {uploadFile("voiture",blob);},"image/jpeg",0.9); //version 1
canvas.toBlob(function(blob) {uploadFile("voiture",new File([blob],"voiture.jpg",{type:blob.type}));},"image/jpeg",0.9); //version 2
});
</script> |
Partager