K
K
KronosHD2015-07-01 13:08:29
Node.js
KronosHD, 2015-07-01 13:08:29

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>

And there is a script - AJAX:
function result() {
   $.ajax({
     'url': "upload/uploader.php",
     'type': "POST",
     'dataType': "html",
     'data': $("#form").serialize(), 
     'success': function(html) {
     $(".result").html(html);
     }
   });
}

But everything is transferred to uploader.php except files. What to do?

UDP: Can you fix it to be correct?
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

2 answer(s)
P
Pavlo Ponomarenko, 2018-10-17
@TheShock

The code is asynchronous, console.log(schedule) is executed the moment you send the request, long before the response arrives.

S
Stalker_RED, 2015-07-01
@Stalker_RED

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).

© stackoverflow.com/questions/166221/how-can-i-uploa...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question