Как загрузить изображение с помощью загрузчика Jodit и Coldfusion? - PullRequest
2 голосов
/ 20 марта 2019

Я использую Jodit для создания редактора wysiwyg.У меня есть файл Coldfusion (uploadimage2.cfm), который пуст.Когда я отправляю загруженное img на эту пустую страницу Coldfusion, я получаю сообщение об ошибке, что coldfusion не может найти переменную "FILES".

Jodit отправляет следующие данные формы в uploadimage2.cfm:

------WebKitFormBoundaryIrkl9oNQedwACmBe
Content-Disposition: form-data; name="path"


------WebKitFormBoundaryIrkl9oNQedwACmBe
Content-Disposition: form-data; name="source"

default
------WebKitFormBoundaryIrkl9oNQedwACmBe
Content-Disposition: form-data; name="files[0]"; filename="HandShake.JPG"
Content-Type: image/jpeg


------WebKitFormBoundaryIrkl9oNQedwACmBe--

Кажется, что Coldfusion застрял в части name = "files [0]".У меня есть рабочая функция загрузки, которая не использует Jodit, и вместо нее отправляется name = "image".

Мне не удалось перехватить данные формы, чтобы попытаться переименовать ее, когда Jodit отправляет ее.

Вот мой javascript с плагином Jodit:

var editor = new Jodit('#newEditor',
   uploader: {
        url: "uploadimage2.cfm",
        filesVariableName: "image"
   }
);

Как я могу отправить правильные данные формы для Coldfusion, чтобы не выдавать ошибки?

1 Ответ

2 голосов
/ 20 марта 2019

В конце концов я понял, что моя проблема была в моем файле application.cfc.Функция onRequest пыталась вычислить "files[0]", чтобы убедиться, что в нее не было вставлено скрипт.Это использовалось для других загрузок текста формы.

Вот как я заставил загрузку Jodit полностью работать с coldfusion:

Мой файл uploadimage2.cfm:

<!--- set content type to json so jodit can read the response --->
<cfheader name="Content-Type" value="application/json">

<!--- create a structure with necessary objects --->
<cfset responseStruct = structNew()>
<cfset responseStruct["message"] = "File was uploaded">
<cfset responseStruct["error"] = 0>
<cfset responseStruct["path"] = "#application.image_root#">
<cfset responseStruct["images"] = []>

<cfset variables.mediapath="#application.image_upload_root#\">

<!--- loop over the form data to upload each image individually --->
<cfloop collection="#form#" item="i">
    <cfif findNoCase("files",i) gte 1>
         <cffile action="upload"
            fileField="#i#"
            destination="#variables.mediapath#"
            accept="image/jpg, image/jpeg, image/png, image/gif, image/svg+xml"
            nameconflict="makeunique"
            result="this_image">

        <cfscript>arrayAppend(responseStruct["images"],"#this_image.serverFile#");</cfscript> 
    </cfif>
</cfloop>

<!--- serialize the structure to json --->    
<cfoutput>#serializeJSON(responseStruct)#</cfoutput>

Тогда в моей инициализации Jodit:

var editor = new Jodit('#editor',
    {
       uploader: {
            url: "uploadimage2.cfm",
                isSuccess: function (resp) {
                //this step is necessary for whatever reason, otherwise it will throw an error.
                    return resp;
                },
                process: function(resp){
                    //this was an important step to align the json with what jodit expects.
                    return {
                        files: resp.images,
                        path: resp.path,
                        baseurl: resp.path,
                        error: resp.error,
                        message: resp.message
                    }
                }
            }
        }
    );
...