如何在 Trumbowyg 中添加 cloudinary 参数以上传插件

how to add cloudinary parameters to upload plugin in Trumbowyg

我在我的项目中使用 trumbowyg 编辑器。从文档中得知可以使用如下代码设置编辑器的图片上传内容

$('#editor')
.trumbowyg({
    btns: ['upload'],
    plugins: {
        // Add imagur parameters to upload plugin for demo purposes
        upload: {
            serverPath: 'https://api.imgur.com/3/image',
            fileFieldName: 'image',
            headers: {
                'Authorization': 'Client-ID xxxxxxxxxxxx'
            },
            urlPropertyName: 'data.link'
        }
    }
});

这适用于 imgur 但我想使用 cloudinary 服务器而不是 imgur

谁能指导我在使用 cloudinary 时如何处理 plugins:{}

我还使用 dropzone.jscloudinary 上传图片,这也能正常工作。 这里是 dropzone 函数代码:

Dropzone.autoDiscover = true;
var myDropzone = new Dropzone(document.getElementById('image-upload'), {
  clickable: "#image-upload #btn-add",
  uploadMultiple: false,
  autoProcessQueue: true,
  acceptedFiles:'.jpg,.png,.jpeg,.gif',
  parallelUploads: 10,
  maxFilesize: 9,
  maxFiles: 10,
  url: 'https://api.cloudinary.com/v1_1/demo_project/image/upload',
  addedfile: function(file) {
    // console.log(file);
    new Noty({
      type: 'success',
      text: "Uploading...",
      timeout: false
    }).show();
    // myDropzone.processQueue();
  },
  success: function(file, response){
    new Noty({
      type: 'success',
      text: "Uploaded!",
      killer: true
    }).show();
    newImageArray.push({
      public_id: response.public_id,
      url: response.url,
      secure_url: response.secure_url
    });
    newImageArrayJSON = JSON.stringify(newImageArray);
    $imageStorage.val(newImageArrayJSON)
    $("#image-upload .image").html('<img src="' + response.secure_url + '">')
    $("#image-upload #btn-add").hide();
    $("#image-upload #btn-remove").show();
  }
});
myDropzone.on('sending', function (file, xhr, formData) {
  formData.append('api_key', 112233445566778);
  formData.append('timestamp', Date.now() / 1000 | 0);
  formData.append('upload_preset', 'mypreset');
});

提前致谢!

我建议从我测试并为我工作的以下基本实现开始:

$('#editor').trumbowyg({
    btns: ['upload'],
    plugins: {
        upload: {
            serverPath: 'https://api.cloudinary.com/v1_1/demo_project/image/upload',
            fileFieldName: 'file',
            urlPropertyName: 'data.secure_url',
            data: [
                {name: 'api_key', value: '112233445566778'},
                {name: 'timestamp', value: Date.now() / 1000 | 0},
                {name: 'upload_preset', value: 'mypreset'}
            ],
            success: function (data) {
                console.log(data);
            },
            error: function (error) {
                console.log(error.responseText);
            }
        }
    }
});

您可以登录您的Cloudinary账户,修改您的上传预设,根据不同的条件限制上传,与dropzone.js相同,例如只允许上传特定格式等