我正在使用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"
}
);
如何为coldfusion发送正确的表单数据以避免错误?
最终我发现我的问题出在我的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
}
}
}
}
);