Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File Upload #2

Open
jnbdz opened this issue Nov 5, 2015 · 1 comment
Open

File Upload #2

jnbdz opened this issue Nov 5, 2015 · 1 comment
Labels

Comments

@jnbdz
Copy link
Member

jnbdz commented Nov 5, 2015

/**
 * Image Ajax Uploader.
 */
function fileAjaxUploader() {}

fileAjaxUploader.prototype = {

    /**
     * Initialise.
     * @retuns {void}
     */
    init: function(dropZone, action, callbackUploadSuccess, callbackUploadFail) {
        this.setDropZone(dropZone);
        this.action = action;

        this._uploadSuccess = (callbackUploadSuccess !== undefined)?callbackUploadSuccess:function(xhr){};
        this._uploadFail = (callbackUploadSuccess !== undefined)?callbackUploadFail:function(xhr){};

        this.setEvents();
    },

    setDropZone: function(dropZone) {
        this.dropZoneId = dropZone;
        this.dropZoneEl = document.getElementById(dropZone);
        console.log(this.dropZoneEl);
    },

    /**
     * Setter for most of the events.
     * @retuns {void}
     */
    setEvents: function() {
        this.dropZoneEl.addEventListener('dragover', function(e) {
            e.preventDefault();
            return false;
        }.bind(this), false);

        this.dropZoneEl.addEventListener('drop', function(e) {
            e.stopPropagation();
            e.preventDefault();

            this.upload(e.dataTransfer.files);
        }.bind(this), false);
    },

    /**
     * Method to upload files.
     * @param {File}
     * @retuns {void}
     */
    upload: function(files) {
        var formData = new FormData(),
            xhr = new XMLHttpRequest();

        for(var i=0;i<files.length;i++) {
            formData.append('images[]', files[i]);
        }

        xhr.open('POST', this.action, true);

        xhr.addEventListener('load', function () {
            if (xhr.status === 200 || xhr.status === 201) {
                this._uploadSuccess(xhr);
            } else {
                this._uploadFail(xhr);
            }
        }.bind(this), false);

        xhr.send(formData);

        return false;
    }
};
@jnbdz jnbdz added the Proposal label Nov 5, 2015
@jnbdz
Copy link
Member Author

jnbdz commented Nov 27, 2015

Interesting lib: http://www.dropzonejs.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant