Answer the question
In order to leave comments, you need to log in
How to send a file via AJAX?
I have a form:
<form id="form" method="POST" enctype="multipart/form-data">
<input type="file" name="files">
<input type="hidden" value="{login}" name="login">
<button class="button" type="button" onclick="result();">Загрузить</button>
</form>
function result() {
$.ajax({
'url': "upload/uploader.php",
'type': "POST",
'dataType': "html",
'data': $("#form").serialize(),
'success': function(html) {
$(".result").html(html);
}
});
}
var filename = $("#file").val();
function result() {
$.ajax({
'url': "upload/uploader.php",
'type': "POST",
'dataType': "html",
'data': {
$("#form").serialize()
file: filename
},
'success': function(html) {
$(".result").html(html);
}
});
}
Answer the question
In order to leave comments, you need to log in
The code is asynchronous, console.log(schedule) is executed the moment you send the request, long before the response arrives.
With HTML5 you CAN make file uploads with Ajax and jQuery. Not only that, you can do file validations (name, size, and MIME-type) or handle the progress event with the HTML5 progress tag (or a div).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question