英文:
When I select the first photo, I can't enter onload, but my code works for the second selection. How can I solve this?
问题
I want to get the photo in the onchange event of my file input element with JavaScript and edit it with canvas, but the photo is processed in onload in the second selection. The first selected photo does not enter the onload event. Thank you for your help in advance
function addTeam_photo() {
const input = document.querySelector("#fileSelect");
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
console.log(input.files[0])
console.log(window.URL.createObjectURL(input.files[0]))
document.getElementById("imgresimguncelle").src = window.URL.createObjectURL(input.files[0]);
input.addEventListener("change", (event) => {
const image = new Image();
image.src = document.getElementById("imgresimguncelle").src;
image.onload = () => {
console.log("onload");
canvas.width = 1000;
canvas.height = 1000;
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
const dataURL = canvas.toDataURL();
console.log(dataURL);
var fd = new FormData();
fd.append("photo", dataURL);
console.log(fd);
$.ajax({
url: "islemler/function.php?islem=addTeam_photo",
type: "POST",
enctype: "multipart/form-data",
processData: false,
contentType: false,
cache: false,
data: fd,
success: function (result) {
if ($.trim(result) == "ok") {
alert("ok");
}
},
});
};
});
}
When I select the first photo, it does not enter onload, but when I select the second, my code works.
英文:
I want to get the photo in the onchange event of my file input element with JavaScript and edit it with canvas, but the photo is processed in onload in the second selection. The first selected photo does not enter the onload event. Thank you for your help in advance
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function addTeam_photo() {
const input = document.querySelector("#fileSelect");
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
console.log(input.files[0])
console.log(window.URL.createObjectURL(input.files[0]))
document.getElementById("imgresimguncelle").src=window.URL.createObjectURL(input.files[0]);
input.addEventListener("change", (event) => {
const image = new Image();
// console.log(event.target.files[0]);
image.src = document.getElementById("imgresimguncelle").src;
image.onload = () => {
console.log("onload");
canvas.width = 1000;
canvas.height = 1000;
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
const dataURL = canvas.toDataURL();
console.log(dataURL);
var fd = new FormData();
fd.append("photo", dataURL);
console.log(fd);
$.ajax({
url: "islemler/function.php?islem=addTeam_photo",
type: "POST",
enctype: "multipart/form-data",
processData: false,
contentType: false,
cache: false,
data: fd,
success: function (result) {
if ($.trim(result) == "ok") {
alert("ok");
}
},
});
};
});
}
<!-- end snippet -->
When I select the first photo, it does not enter onload, but when I select the second, my code works
答案1
得分: 0
你应该在设置任何图像源之前设置onchange
事件处理程序。可能发生的情况是,在设置更改事件处理程序之前,你的第一个图像已完全加载。
function addTeam_photo() {
const input = document.querySelector("#fileSelect");
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
console.log(input.files[0]);
console.log(window.URL.createObjectURL(input.files[0]));
document.getElementById("imgresimguncelle").src = window.URL.createObjectURL(input.files[0]);
input.addEventListener("change", (event) => {
const image = new Image();
// console.log(event.target.files[0]);
// 在设置图像源之前设置加载事件,使用.addEventListener而不是.onchange
image.addEventListener("change", () => {
console.log("onload");
canvas.width = 1000;
canvas.height = 1000;
ctx.drawImage(image, 0, 0, canvas.width, canvas height);
const dataURL = canvas.toDataURL();
console.log(dataURL);
var fd = new FormData();
fd.append("photo", dataURL);
console.log(fd);
$.ajax({
url: "islemler/function.php?islem=addTeam_photo",
type: "POST",
enctype: "multipart/form-data",
processData: false,
contentType: false,
cache: false,
data: fd,
success: function (result) {
if ($.trim(result) == "ok") {
alert("ok");
}
},
});
});
// 现在有了图像加载事件的监听器,
// 开始加载图像。
image.src = document.getElementById("imgresimguncelle").src;
});
}
英文:
You should be setting up the onchange
event handlers before setting up any image sources. What's probably happening is your first image is fully loaded before you set up the change event handler.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function addTeam_photo() {
const input = document.querySelector("#fileSelect");
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
console.log(input.files[0])
console.log(window.URL.createObjectURL(input.files[0]))
document.getElementById("imgresimguncelle").src=window.URL.createObjectURL(input.files[0]);
input.addEventListener("change", (event) => {
const image = new Image();
// console.log(event.target.files[0]);
// Set up the load event before setting the image source
// And use .addEventListener instead of .onchange
image.addEventListener("change", () => {
console.log("onload");
canvas.width = 1000;
canvas.height = 1000;
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
const dataURL = canvas.toDataURL();
console.log(dataURL);
var fd = new FormData();
fd.append("photo", dataURL);
console.log(fd);
$.ajax({
url: "islemler/function.php?islem=addTeam_photo",
type: "POST",
enctype: "multipart/form-data",
processData: false,
contentType: false,
cache: false,
data: fd,
success: function (result) {
if ($.trim(result) == "ok") {
alert("ok");
}
},
});
});
// Now that there is a listener for the image load event,
// start to load an image.
image.src = document.getElementById("imgresimguncelle").src;
});
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论