Skip to content

Instantly share code, notes, and snippets.

@webber12
Created November 24, 2022 17:12
Show Gist options
  • Save webber12/0ded1bf459628eef53bf68a0f43d0149 to your computer and use it in GitHub Desktop.
Save webber12/0ded1bf459628eef53bf68a0f43d0149 to your computer and use it in GitHub Desktop.
Отправка файлов ajax
//<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