Created
November 24, 2022 17:12
-
-
Save webber12/0ded1bf459628eef53bf68a0f43d0149 to your computer and use it in GitHub Desktop.
Отправка файлов ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//<input type="file" data-upload-file> | |
//<input type="file" multiple data-upload-file> | |
//ловим в файле ajax.php $_FILES['files'] и $_POST['action'] == "uploadFiles" | |
$(document).on("change", "[data-upload-file]", function(e){ | |
e.preventDefault(); | |
const formData = new FormData(); | |
formData.append("action", "uploadFiles"); | |
for(let k = 0; k < e.target.files.length; k++) { | |
formData.append("files[" + k + "]", e.target.files[k]); | |
} | |
$.ajax({ | |
url: "ajax.php", | |
data: formData, | |
type: "POST", | |
cache: false, | |
processData: false, | |
contentType: false, | |
dataType: 'json', | |
beforeSend: function () { | |
}, | |
success: function (msg) { | |
//console.log(msg); | |
} | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment