index.html 1.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<!DOCTYPE html>
<html>
<head>
  <title>AJAX Upload Example</title>
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  <script type="text/javascript">
    function updateProgress(evt) {
      if (evt.lengthComputable) {
        document.getElementById("output").textContent =
              "Uploaded " + evt.loaded + " of " + evt.total + " bytes";
      }
    }
    function uploadFile() {
      var file_data = new FormData(document.getElementById('filename'));
      $.ajax({
        url: "/upload",
        type: "POST",
        data: file_data,
        processData: false,
        contentType: false,
        cache: false,
        xhr: function() {
          myXhr = $.ajaxSettings.xhr();
          if(myXhr.upload){
            myXhr.upload.addEventListener('progress',updateProgress, false); // for handling the progress of the upload
          }
          return myXhr;
        },
      }).done(function(data) {
          document.getElementById("output").textContent = "Result: " + data;
      });
      return false;
    }
  </script>
</head>

<body>
Sergey Lyubka's avatar
Sergey Lyubka committed
38 39 40 41 42 43 44 45 46
  <h1>Upload file using standard form upload</h1>

  <form method="POST" action="/upload" enctype="multipart/form-data">
    <label>Select a file:</label><br>
    <input type="file" name="file" />
    <input type="submit" value="Upload" />
  </form>

  <h1>Upload file using Ajax - that also gives progress report</h1>
47 48 49 50 51 52 53 54
  <form method="post" id="filename" name="filename" onsubmit="return uploadFile();">
    <label>Select a file:</label><br>
    <input type="file" id="file" name="file" required />
    <input type="submit" value="Upload" />
  </form>
  <br><br><div id="output"></div>
</body>
</html>