我正在尝试使用 jquery ajax 方法发送文件,但我不知道如何在 Coldfusion 中接收它,将其转换为适当的文件对象,最后将其保存在 Media 文件夹中的服务器上。
有我想要的东西:
var myDocument = $('#fileinput').prop('files');
var formData = new FormData();
formData.append('myDocument', myDocument);
$.ajax({
url: "./somePath/file.cfc?method=handleDocument",
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function(result,status,xhr){
doSomething();
},
error: function(xhr,status,error){
doSomethingElse();
}
});
现在,我们在./somePath/file.cfc中尝试获取myDocument对象并保存它:
<cffunction name="handleDocument" access="remote" returnformat="JSON" returntype="string">
<cfset requestData = GetHttpRequestData()>
<cfset content = requestData.content>
<cfset anyExtesion= content.getExtension()>
<!---There's no such method getExtension(), but I need something like that--->
<cffile action="write" file="./Media/myDocument.#anyExtesion#" output="#x.content#"/>
<cfreturn "File is saved in Media folder.">
</cffunction>