图片上传 cfm 或 cfc for Redactor editor
Image upload cfm or cfc for Redactor editor
我正在尝试使用简单的图像上传脚本为 ColdFusion 配置编辑器。该图像实际上并没有上传到服务器,我不知道如何将变量返回到 ColdFusion 以供 redactor 使用。我似乎无法 their sample code 正确转换为 ColdFusion 。
我对函数的调用在这里:
this.article_content.redactor({ focus: true,
buttons: [ 'html', '|', 'formatting', '|', 'bold', 'italic', 'underline', '|', 'unorderedlist', 'orderedlist', '|',
'image', 'video', 'file', 'link', '|',
'fontcolor', '|', 'alignment', '|', 'horizontalrule', 'alignleft', 'aligncenter', 'alignright', 'justify'],
imageUpload: 'lib/modules/imageUpload.cfm'
});
imageUpload.cfm
<cfparam name="form.file" default="">
<cffile action="upload" destination="#dir#" accept="image/*" nameconflict="overwrite" filefield="file"/>
<cfset result = structnew()>
<cfset result["name"] = cffile.serverFile>
<cfset result["url"] = cffile.serverFile>
<cfset result["filelink"] = cffile.serverFile>
<cfset result["type"] = cffile.ContentType &"/"& cffile.ContentSubType>
<cfset result["size"] = cffile.FileSize>
<cfreturn serializejson(result)>
这应该是 cfm 页面还是 cfc 页面?我基本上只需要将 serverFile 变量转换为如下所示的 json 对象:
{ "url": "/images/img.jpg", "id": "123" }
我觉得你们很亲密!这应该可以解决问题:
<cfsetting enablecfoutputonly="true">
<!---
Your location to upload files to
Ideally this would be outside the webroot so you can upload the file, check
to ensure it's not malicious, then move it to a publicly accessible location
--->
<cfset relativePath = "uploads/">
<cfset dir = expandPath(relativePath)>
<cfset result = {}>
<cfif structKeyExists(form, "file")>
<cffile action="upload" destination="#dir#" accept="image/*" nameconflict="overwrite" filefield="file">
<cfset result["name"] = cffile.serverFile>
<cfset result["url"] = relativePath&cffile.serverFile>
<cfset result["filelink"] = cffile.serverFile>
<cfset result["type"] = cffile.ContentType&"/"&cffile.ContentSubType>
<cfset result["size"] = cffile.FileSize>
<cfelse>
<cfset result["error"] = "no file uploaded">
</cfif>
<cfheader name="Content-Type" value="application/json">
<cfoutput>#serializejson(result)#</cfoutput>
<cfsetting enablecfoutputonly="false">
主要区别在于,对于 .cfm 页面,您可以只输出 JSON,而您试图 return 它(使用 cfreturn),这就是您想要的d 从一个函数做。
我添加了 enablecfoutputonly
设置以确保您不会得到任何会破坏 JSON 响应的不需要的空格。
您可以尝试添加内容类型为 application/json
的 cfheader
。正如@beloitdavisja 建议的那样,将 cfreturn
替换为 cfoutput
。除非您在函数内部,否则不要在 cfm 页面中使用 cfreturn
。像这样:
<cfheader name="Content-Type" value="application/json">
<cfoutput>#serializejson(result)#</cfoutput>
我正在尝试使用简单的图像上传脚本为 ColdFusion 配置编辑器。该图像实际上并没有上传到服务器,我不知道如何将变量返回到 ColdFusion 以供 redactor 使用。我似乎无法 their sample code 正确转换为 ColdFusion 。
我对函数的调用在这里:
this.article_content.redactor({ focus: true,
buttons: [ 'html', '|', 'formatting', '|', 'bold', 'italic', 'underline', '|', 'unorderedlist', 'orderedlist', '|',
'image', 'video', 'file', 'link', '|',
'fontcolor', '|', 'alignment', '|', 'horizontalrule', 'alignleft', 'aligncenter', 'alignright', 'justify'],
imageUpload: 'lib/modules/imageUpload.cfm'
});
imageUpload.cfm
<cfparam name="form.file" default="">
<cffile action="upload" destination="#dir#" accept="image/*" nameconflict="overwrite" filefield="file"/>
<cfset result = structnew()>
<cfset result["name"] = cffile.serverFile>
<cfset result["url"] = cffile.serverFile>
<cfset result["filelink"] = cffile.serverFile>
<cfset result["type"] = cffile.ContentType &"/"& cffile.ContentSubType>
<cfset result["size"] = cffile.FileSize>
<cfreturn serializejson(result)>
这应该是 cfm 页面还是 cfc 页面?我基本上只需要将 serverFile 变量转换为如下所示的 json 对象:
{ "url": "/images/img.jpg", "id": "123" }
我觉得你们很亲密!这应该可以解决问题:
<cfsetting enablecfoutputonly="true">
<!---
Your location to upload files to
Ideally this would be outside the webroot so you can upload the file, check
to ensure it's not malicious, then move it to a publicly accessible location
--->
<cfset relativePath = "uploads/">
<cfset dir = expandPath(relativePath)>
<cfset result = {}>
<cfif structKeyExists(form, "file")>
<cffile action="upload" destination="#dir#" accept="image/*" nameconflict="overwrite" filefield="file">
<cfset result["name"] = cffile.serverFile>
<cfset result["url"] = relativePath&cffile.serverFile>
<cfset result["filelink"] = cffile.serverFile>
<cfset result["type"] = cffile.ContentType&"/"&cffile.ContentSubType>
<cfset result["size"] = cffile.FileSize>
<cfelse>
<cfset result["error"] = "no file uploaded">
</cfif>
<cfheader name="Content-Type" value="application/json">
<cfoutput>#serializejson(result)#</cfoutput>
<cfsetting enablecfoutputonly="false">
主要区别在于,对于 .cfm 页面,您可以只输出 JSON,而您试图 return 它(使用 cfreturn),这就是您想要的d 从一个函数做。
我添加了 enablecfoutputonly
设置以确保您不会得到任何会破坏 JSON 响应的不需要的空格。
您可以尝试添加内容类型为 application/json
的 cfheader
。正如@beloitdavisja 建议的那样,将 cfreturn
替换为 cfoutput
。除非您在函数内部,否则不要在 cfm 页面中使用 cfreturn
。像这样:
<cfheader name="Content-Type" value="application/json">
<cfoutput>#serializejson(result)#</cfoutput>