tranlongpc
3/5/2018 - 7:22 AM

[js upload file on tinymce]

[js upload file on tinymce]

file_picker_callback: function(cb, value, meta) {

        var input = document.createElement('input');
        input.setAttribute('type', 'file');
        input.setAttribute('accept', 'image/*');

        input.onchange = function() {
            console.log(this.files);
            var file = this.files[0];

            console.log( meta ); //I thought it might be here in the meta bt it isn't

            console.log( $('#mceu_62').val() ); //I tried to get it from its id but it returns undefined i think that field is created after this function is created.



            var id = file.name;
            var blobCache = tinymce.activeEditor.editorUpload.blobCache;
            var blobInfo = blobCache.create(id, file);
            blobCache.add(blobInfo);


            // call the callback and populate the Title field with the file name
            cb(blobInfo.blobUri(), { title: file.name });
        };

        input.click();
    },
    images_upload_handler: function (blobInfo, success, failure) {
        var xhr, formData;

        xhr = new XMLHttpRequest();
        xhr.withCredentials = false;
        xhr.open('POST', DOMAIN_API + '?mod=file&cmd=Upload');

        xhr.onload = function() {
            var json;

            if (xhr.status != 200) {
                failure('HTTP Error: ' + xhr.status);
                return;
            }

            json = JSON.parse(xhr.responseText);

            if (!json || typeof json.location != 'string') {
                failure('Invalid JSON: ' + xhr.responseText);
                return;
            }

            success(json.location);
        };


        var description = '';

        formData = new FormData();
        formData.append('file', blobInfo.blob(), blobInfo.filename());
        formData.append('description', description); //found now))
        xhr.send(formData);
    }