javascript - 如何使用 Jodit 上传器和 Coldfusion 上传图像?
问题描述
我正在使用Jodit创建一个所见即所得的编辑器。我有一个空的冷融合文件(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--
似乎冷融合卡在 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 上传完整地与冷融合一起工作:
我的 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
}
}
}
}
);
推荐阅读
- sql - 为sql server中的重复值生成ID
- c++ - 从类中返回一个函数
- http - 是否可以在浏览器中为 Back-Forward 缓存设置超时?
- libcurl - curl_easy_perform GET 请求中的空 Content-Type
- image - 如何解码base64 fbx嵌入图像
- python-3.x - 使用请求从 ServiceDesk 请求中抓取数据
- http - 如何设置 Cloudflare 防火墙以阻止假 Google 机器人
- sql - 在由唯一 ID 标识的列中选择多个值并将它们解析为一行
- php - 注意:未定义变量:twig
- css - 通过 AJAX 动态创建内容时分页符后不起作用