Skip to content

Instantly share code, notes, and snippets.

@phpfour
Created October 2, 2012 13:23
Show Gist options
  • Select an option

  • Save phpfour/3819085 to your computer and use it in GitHub Desktop.

Select an option

Save phpfour/3819085 to your computer and use it in GitHub Desktop.

Revisions

  1. phpfour created this gist Oct 2, 2012.
    41 changes: 41 additions & 0 deletions upload.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    (function () {

    var input = document.getElementById("image"), formdata = false;

    if (window.FormData) {
    formdata = new FormData();
    }

    input.addEventListener("change", function (evt) {

    file = this.files[0];

    if (!!file.type.match(/image.*/)) {
    if ( window.FileReader ) {
    reader = new FileReader();
    reader.onloadend = function (e) {
    //showUploadedItem(e.target.result, file.fileName);
    };
    reader.readAsDataURL(file);
    }
    if (formdata) {
    formdata.append("image", file);
    }
    }

    if (formdata) {
    $.ajax({
    url: "upload.php",
    type: "POST",
    data: formdata,
    processData: false,
    contentType: false,
    success: function (res) {
    window.uploadedImage = res;
    $('#image-holder').append('<img src="' + window.uploadedImage + '" />');
    }
    });
    }
    }, false);

    }());