When I select the first photo, I can't enter onload, but my code works for the second selection. How can I solve this?

huangapple go评论61阅读模式
英文:

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(&quot;#fileSelect&quot;);
  const canvas = document.createElement(&quot;canvas&quot;);
  const ctx = canvas.getContext(&quot;2d&quot;);
  console.log(input.files[0])
  console.log(window.URL.createObjectURL(input.files[0]))
  document.getElementById(&quot;imgresimguncelle&quot;).src=window.URL.createObjectURL(input.files[0]);
  input.addEventListener(&quot;change&quot;, (event) =&gt; {
    const image = new Image();
    // console.log(event.target.files[0]);
    image.src = document.getElementById(&quot;imgresimguncelle&quot;).src;
    image.onload = () =&gt; {
      console.log(&quot;onload&quot;);
      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(&quot;photo&quot;, dataURL);
      console.log(fd);
      $.ajax({
        url: &quot;islemler/function.php?islem=addTeam_photo&quot;,
        type: &quot;POST&quot;,
        enctype: &quot;multipart/form-data&quot;,
        processData: false,
        contentType: false,
        cache: false,
        data: fd,
        success: function (result) {
          if ($.trim(result) == &quot;ok&quot;) {
            alert(&quot;ok&quot;);
          }
        },
      });
    };
  });
}

<!-- 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(&quot;#fileSelect&quot;);
const canvas = document.createElement(&quot;canvas&quot;);
const ctx = canvas.getContext(&quot;2d&quot;);
console.log(input.files[0])
console.log(window.URL.createObjectURL(input.files[0]))
document.getElementById(&quot;imgresimguncelle&quot;).src=window.URL.createObjectURL(input.files[0]);
input.addEventListener(&quot;change&quot;, (event) =&gt; {
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(&quot;change&quot;, () =&gt; {
console.log(&quot;onload&quot;);
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(&quot;photo&quot;, dataURL);
console.log(fd);
$.ajax({
url: &quot;islemler/function.php?islem=addTeam_photo&quot;,
type: &quot;POST&quot;,
enctype: &quot;multipart/form-data&quot;,
processData: false,
contentType: false,
cache: false,
data: fd,
success: function (result) {
if ($.trim(result) == &quot;ok&quot;) {
alert(&quot;ok&quot;);
}
},
});
});
// Now that there is a listener for the image load event,
// start to load an image.
image.src = document.getElementById(&quot;imgresimguncelle&quot;).src;
});
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 19:43:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定