问题描述
限时送ChatGPT账号..TL:DR 我正在尝试使用 javascript 获取 image_description 字段的值以将其传递给我的 post xhr 请求
TL:DR I am trying to get the value of image_description field using javascript to past it my post xhr request
下面的原始问题
我正在使用 file_picker_callback 类型的图像https://www.tinymce/docs/configure/file-image-upload/#file_picker_callback
I am using file_picker_callback type image https://www.tinymce/docs/configure/file-image-upload/#file_picker_callback
我在我的
tinymce.init({
....,
image_description: true,
...
一切正常上传,但我想传递 image_description 字段以及存储在数据库中.但我无法获取数据
Everything is uploading fine but I want to pass the image_description field as well to store in the DB. But i can't grab the data
这里是我的两个函数,直接取自tinymce网站
Here are my two functions, taking directly from the tinymce website
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', '/articles/postAcceptor');
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);
};
formData = new FormData();
formData.append('file', blobInfo.blob(), blobInfo.filename());
formData.append('description', /*but i can't get the value*/);
xhr.send(formData);
}
@卡尔莫里森
推荐答案
试试这个以获得价值:
images_upload_handler: function (blobInfo, success, failure) {
var xhr, formData;
xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open('POST', '/articles/postAcceptor');
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 = '';
jQuery(tinymce.activeEditor.dom.getRoot()).find('img').not('.loaded-before').each(
function() {
description = $(this).attr("alt");
$(this).addClass('loaded-before');
});
formData = new FormData();
formData.append('file', blobInfo.blob(), blobInfo.filename());
formData.append('description', description); //found now))
xhr.send(formData);
}
这篇关于Tinymce 使用 file_picker_callback 和图像上传器获取图像 _description的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论