如何使用Jodit上传器和Coldfusion上传图像?

时间:2019-03-19 21:06:35

标签: javascript typescript coldfusion image-uploading jodit

我正在使用Jodit创建一个所见即所得的编辑器。我有一个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发送数据时,我无法拦截表单数据以尝试重命名。

这是我的带有Jodit插件的JavaScript:

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

如何发送正确的表单数据以进行冷熔融合,以免出错?

1 个答案:

答案 0 :(得分:2)

最终,我发现问题出在我的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
                    }
                }
            }
        }
    );