gpt4 book ai didi

javascript - 文件输入的输入事件和/或更改事件在 Chrome 和 Opera 中触发,但在 Firefox、Edge 或 IE11 中不触发

转载 作者:行者123 更新时间:2023-12-03 00:48:23 25 4
gpt4 key购买 nike

我创建了一个拖放文件 uploader ,它在 Chrome 和 Opera 中运行良好。然而 Firefox、Edge 和 Internet Explorer 却让我头疼。

我有以下最小示例(为了方便起见,使用控制台日志记录):

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
console.log("Document is ready.");

document.addEventListener("drop", function(e) { // Fires on drop event
console.log("Drop event recorded on document.");
input = document.querySelector("#file-input"); // Find file input
input.files = e.dataTransfer.files; // Set dropped files into file input
e.preventDefault(); // Prevent default behavior (like opening the file(s) in the browser tab)
});

document.addEventListener("dragover", function(e) {
console.log("Dragover event recorded on document.");
e.preventDefault(); // Prevent default behavior (like opening the file(s) in the browser tab)
});
});

function uploadFiles() {
console.log("Function uploadFiles() called, it works in this browser!");
// File upload logic here
alert("Got new file(s), it works in this browser!");
}
</script>
</head>
<body>
<h1>Test input event</h1>
<p>Drop file(s) anywhere on the page to trigger the input event. The event is fired as expected in Chrome and Opera, but not in Firefox, Edge or Internet Explorer 11.</p>
<p>When the event is fired, you will be notified by an alert message.</p>
<input type="file" multiple id="file-input" oninput="console.log('oninput fired on input element.'); uploadFiles();" disabled/>
</body>
</html>

Chrome 和 Opera 按照我们的预期运行此页面。这些事件会在适当的时间触发,并最终调用 uploadFiles() 函数。

Firefox、Edge 和 IE 能够触发拖放和放置事件,但当从 dataTransfer 插入文件时,不会触发输入元素上的输入事件。

我搜索了一整天,发现了几个类似的问题,但所提出的解决方案都不适合我。

最佳答案

附加文件内容后触发input事件,或者更好地在drop事件中调用uploadFiles

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
console.log("Document is ready.");

document.addEventListener("drop", function(e) { // Fires on drop event
console.log("Drop event recorded on document.");
input = document.querySelector("#file-input"); // Find file input
input.files = e.dataTransfer.files; // Set dropped files into file input
$("#file-input").trigger('input');//trigger the upload
e.preventDefault(); // Prevent default behavior (like opening the file(s) in the browser tab)
});

document.addEventListener("dragover", function(e) {
console.log("Dragover event recorded on document.");
e.preventDefault(); // Prevent default behavior (like opening the file(s) in the browser tab)
});
});

function uploadFiles() {
console.log("Function uploadFiles() called, it works in this browser!");
// File upload logic here
alert("Got new file(s), it works in this browser!");
}
</script>
</head>
<body>
<h1>Test input event</h1>
<p>Drop file(s) anywhere on the page to trigger the input event. The event is fired as expected in Chrome and Opera, but not in Firefox, Edge or Internet Explorer 11.</p>
<p>When the event is fired, you will be notified by an alert message.</p>
<input type="file" multiple id="file-input" oninput="console.log('oninput fired on input element.'); uploadFiles();" disabled/>
</body>
</html>

关于javascript - 文件输入的输入事件和/或更改事件在 Chrome 和 Opera 中触发,但在 Firefox、Edge 或 IE11 中不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53153890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com