Sie haben die image plugin für TinyMCE verwenden und file_picker
Eigenschaften und Rückrufe, so dass Sie Dateien von Client-Seite anhängen können, anstatt URL.
tinymce.init({
// Include image plugin on plugin list
plugins: [ 'image'],
// Include image button on toolbar
toolbar: ['image'],
// Enable title field in the Image dialog
image_title: true,
// Enable automatic uploads of images represented by blob or data URIs
automatic_uploads: true,
// URL of your upload handler
// (YOU SHOULD MAKE AN ENDPOINT TO RECEIVE THIS AND RETURN A JSON CONTAINING: {location: remote_image_url})
images_upload_url: '/text_images',
// Here we add custom filepicker only to Image dialog
file_picker_types: 'image',
// And here's your custom image picker
file_picker_callback: function(cb, value, meta) {
var input = document.createElement('input');
input.setAttribute('type', 'file');
input.setAttribute('accept', 'image/*');
input.onchange = function() {
var file = this.files[0];
// Note: Now we need to register the blob in TinyMCEs image blob
// registry.
var id = 'blobid' + (new Date()).getTime();
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();
}
});
hinzufügen text_images
auf Ihre route.rb
Datei:
match "text_images" => "text_images#create", via: :post
Und Ihre verarbeitend Aktion wie folgt erstellen:
def create
if params[:file].class == ActionDispatch::Http::UploadedFile
@image = Picture.new(image: params[:file])
respond_to do |format|
if @image.save
format.json { render json: { "location": @image.image.url }.to_json, status: :ok }
else
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
end
Dies ist eine sehr grobe Implementierung, sollten Sie es sicherer machen für Ihr Anwendungskontext, Validierung und Filterung großer oder ungültiger Dateien!
UPDATE: Es gab kürzlich eine Aktualisierung auf die Syntax für neue Versionen von TinyMCE für die onchange
Funktion ein Ergebnis Leser Attribut auf der create-Methode des blobCache
Objekt gehören:
input.onchange = function() {
var file = this.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function() {
// Note: Now we need to register the blob in TinyMCEs image blob
// registry. In the next release this part hopefully won't be
// necessary, as we are looking to handle it internally.
var id = 'blobid' + (new Date()).getTime();
var blobCache = tinymce.activeEditor.editorUpload.blobCache;
var blobInfo = blobCache.create(id, file, reader.result);
blobCache.add(blobInfo);
// call the callback and populate the Title field with the file name
cb(blobInfo.blobUri(), { title: file.name });
};
};
Thank you so weit für das detaillierte Antwort. Ich werde es ausprobieren und deine Antwort als richtig markieren, wenn es funktioniert :) – Mainz007
Ok deine Antwort brachte mich weiter, löste aber nicht das Problem, dass ich beim Hochladen des Bildes eine CSRF-Warnung hatte. Ich musste meine benutzerdefinierte Funktion images_upload_handler schreiben. Auf diese Weise konnte ich das csrf_token hinzufügen, das RoR generiert. Jetzt wirft die Anwendung die Warnung nicht mehr und beendet die Sitzung. Ich werde am Wochenende meine endgültige Lösung schreiben. – Mainz007
Sie sollten Ihr Leben einfacher machen, nie wieder in diese Probleme laufen http://stackoverflow.com/a/24196317/3399504 – ErvalhouS