Skip to content
Extraits de code Groupes Projets
fileuploader-custom.js 36,9 ko
Newer Older
  • Learn to ignore specific revisions
  •             // to cancel request set src to something else
                // we use src="javascript:false;" because it doesn't
                // trigger ie6 prompt on https
                iframe.setAttribute('src', 'javascript:false;');
    
                qq.remove(iframe);
            }
    
            if (!input){
                throw new Error('file with passed id was not added, or already uploaded or cancelled');
    
            var iframe = this._createIframe(id);
            var form = this._createForm(iframe, params);
            form.appendChild(input);
    
            $(form).append($('<input type="hidden" name="authenticity_token" value="'+$("meta[name='csrf-token']").attr("content")+'"/>'));
    
            this._attachLoadEvent(iframe, function(){
    
                self.log('iframe loaded');
    
                var response = self._getIframeContentJSON(iframe);
    
                self._options.onComplete(id, fileName, response);
                self._dequeue(id);
    
                delete self._inputs[id];
                // timeout added to fix busy state in FF3.6
                setTimeout(function(){
                    qq.remove(iframe);
                }, 1);
            });
    
    
            return id;
    
        _attachLoadEvent: function(iframe, callback){
            qq.attach(iframe, 'load', function(){
                // when we remove iframe from dom
                // the request stops, but in IE load
                // event fires
                if (!iframe.parentNode){
                    return;
                }
    
                // fixing Opera 10.53
                if (iframe.contentDocument &&
                    iframe.contentDocument.body &&
                    iframe.contentDocument.body.innerHTML == "false"){
                    // In Opera event is fired second time
                    // when body.innerHTML changed from false
                    // to server response approx. after 1 sec
                    // when we upload file with iframe
                    return;
                }
    
                callback();
            });
        },
        /**
         * Returns json object received by iframe from server.
         */
        _getIframeContentJSON: function(iframe){
            // iframe.contentWindow.document - for IE<7
            var doc = iframe.contentDocument ? iframe.contentDocument: iframe.contentWindow.document,
                response;
    
            this.log("converting iframe's innerHTML to JSON");
            this.log("innerHTML = " + doc.body.innerHTML);
    
            try {
    
                response = eval("(" + doc.body.innerHTML + ")");
            } catch(err){
                response = {};
    
    
            return response;
        },
        /**
         * Creates iframe with unique name
         */
        _createIframe: function(id){
            // We can't use following code as the name attribute
            // won't be properly registered in IE6, and new window
            // on form submit will open
            // var iframe = document.createElement('iframe');
            // iframe.setAttribute('name', id);
    
            var iframe = qq.toElement('<iframe src="javascript:false;" name="' + id + '" />');
            // src="javascript:false;" removes ie6 prompt on https
    
            iframe.setAttribute('id', id);
    
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
    
            return iframe;
        },
        /**
         * Creates form, that will be submitted to iframe
         */
        _createForm: function(iframe, params){
            // We can't use the following code in IE6
            // var form = document.createElement('form');
            // form.setAttribute('method', 'post');
            // form.setAttribute('enctype', 'multipart/form-data');
            // Because in this case file won't be attached to request
            var form = qq.toElement('<form method="post" enctype="multipart/form-data"></form>');
    
    
            var queryString = qq.obj2url(params, this._options.action);
    
            form.setAttribute('action', queryString);
    
            form.setAttribute('target', iframe.name);
            form.style.display = 'none';
            document.body.appendChild(form);
    
            return form;
        }
    
     * @inherits qq.UploadHandlerAbstract
    
        qq.UploadHandlerAbstract.apply(this, arguments);
    
    
        // current loaded size in bytes for each file
    
        this._loaded = [];
    
    };
    
    // static method
    qq.UploadHandlerXhr.isSupported = function(){
        var input = document.createElement('input');
    
        return (
            'multiple' in input &&
            typeof File != "undefined" &&
    
            typeof (new XMLHttpRequest()).upload != "undefined" );
    
    // @inherits qq.UploadHandlerAbstract
    qq.extend(qq.UploadHandlerXhr.prototype, qq.UploadHandlerAbstract.prototype)
    
    qq.extend(qq.UploadHandlerXhr.prototype, {
    
        /**
         * Adds file to the queue
         * Returns id to use with upload, cancel
    
            if (!(file instanceof File)){
                throw new Error('Passed obj in not a File (in qq.UploadHandlerXhr)');
            }
    
            var file = this._files[id];
            // fix missing name in Safari 4
    
            return file.fileName != null ? file.fileName : file.name;
    
        },
        getSize: function(id){
            var file = this._files[id];
            return file.fileSize != null ? file.fileSize : file.size;
    
         * Returns uploaded bytes for file identified by id
         */
    
        getLoaded: function(id){
    
        /**
         * Sends the file identified by id and additional query params to the server
         * @param {Object} params name-value string pairs
    
        _upload: function(id, params){
    
            var file = this._files[id],
                name = this.getName(id),
                size = this.getSize(id);
    
            this._loaded[id] = 0;
    
            var xhr = this._xhrs[id] = new XMLHttpRequest();
            var self = this;
    
            xhr.upload.onprogress = function(e){
                if (e.lengthComputable){
    
                    self._loaded[id] = e.loaded;
    
                    self._options.onProgress(id, name, e.loaded, e.total);
                }
            };
    
    
            params = params || {};
            params['qqfile'] = name;
            var queryString = qq.obj2url(params, this._options.action);
    
            xhr.open("POST", queryString, true);
    
            xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
            xhr.setRequestHeader("X-File-Name", encodeURIComponent(name));
            xhr.setRequestHeader("Content-Type", "application/octet-stream");
    
            xhr.setRequestHeader("X-CSRF-Token", $("meta[name='csrf-token']").attr("content"));
    
            xhr.setRequestHeader("Accept", "application/json");
    
        _onComplete: function(id, xhr){
            // the request was aborted/cancelled
            if (!this._files[id]) return;
    
            var name = this.getName(id);
            var size = this.getSize(id);
    
            this._options.onProgress(id, name, size, size);
    
            if (xhr.status == 200){
                this.log("xhr - server response received");
                this.log("responseText = " + xhr.responseText);
    
                var response;
    
                try {
                    response = eval("(" + xhr.responseText + ")");
                } catch(err){
                    response = {};
                }
    
    
                this._completed_files.push({file: this._files[id], response: response});
    
                this._options.onComplete(id, name, response);
    
    
            } else {
                this._completed_files.push({file: this._files[id], response: {}});
    
                this._options.onComplete(id, name, {});
    
            this._files[id] = null;
    
        _cancel: function(id){
            this._options.onCancel(id, this.getName(id));
    
            this._files[id] = null;
    
            if (this._xhrs[id]){
                this._xhrs[id].abort();
    
    Paul Hill's avatar
    Paul Hill a validé
    // @license-end