Skip to content

Instantly share code, notes, and snippets.

@spir1donov
Forked from nilcolor/ajax-file-upload.js
Created June 29, 2017 07:14
Show Gist options
  • Save spir1donov/ecbf3da3525f5d7003d3c1cf07ab8a9d to your computer and use it in GitHub Desktop.
Save spir1donov/ecbf3da3525f5d7003d3c1cf07ab8a9d to your computer and use it in GitHub Desktop.

Revisions

  1. @nilcolor nilcolor revised this gist Jul 3, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions sample-form.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    <form id="cpd-file" action="/upload_file" method="post" enctype="multipart/form-data">
    <input type="file" name="cpd_file">
    </form>
  2. @nilcolor nilcolor created this gist Jul 3, 2012.
    15 changes: 15 additions & 0 deletions ajax-file-upload.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    function sendForm() {
    var fileData = new FormData(document.getElementById("cpd-file"));
    var xhr = new XMLHttpRequest();

    xhr.open("POST", "/upload_file", true);
    xhr.setRequestHeader('Accept', 'application/json');
    xhr.onload = function(progressEvent) {
    if (xhr.status == 200) {
    console.log('Uploaded', xhr);
    } else {
    console.log("Error " + xhr.status + " occurred uploading your file.", xhr);
    }
    };
    xhr.send(oData);
    }