Using the File APIs

Alert:

All mobile and mobile-related features are removed in ColdFusion (2025 release).

View Deprecated features in ColdFusion for more information.

Before you begin, read File System Functions.

Uploading a file from the device

<cfclientsettings enabledeviceapi="true" enabledebuglog="true">
<cfclient>
<cffunction name="uploadSuccess" >
<cfset alert("uploaded")>
</cffunction>
<cffunction name="uploadError" >
<cfargument name="err" >
<cfset alert(err.code)>
</cffunction>
<cftry>
<cfscript>
cfclient.file.write("a.txt","hello");
cfclient.file.upload("a.txt","http://<Your-CF-Server>:8500/fileUpload/files/upload.cfm",uploadSuccess,uploadError);
</cfscript>
<cfcatch type="Any" >
<cfset alert("exception " & cfcatch.message)>
</cfcatch>
</cftry>
</cfclient>
<cfclientsettings enabledeviceapi="true" enabledebuglog="true"> <cfclient> <cffunction name="uploadSuccess" > <cfset alert("uploaded")> </cffunction> <cffunction name="uploadError" > <cfargument name="err" > <cfset alert(err.code)> </cffunction> <cftry> <cfscript> cfclient.file.write("a.txt","hello"); cfclient.file.upload("a.txt","http://<Your-CF-Server>:8500/fileUpload/files/upload.cfm",uploadSuccess,uploadError); </cfscript> <cfcatch type="Any" > <cfset alert("exception " & cfcatch.message)> </cfcatch> </cftry> </cfclient>
<cfclientsettings enabledeviceapi="true" enabledebuglog="true">

<cfclient>

<cffunction name="uploadSuccess" >
<cfset alert("uploaded")>
</cffunction>

<cffunction name="uploadError" >
<cfargument name="err" >
<cfset alert(err.code)>
</cffunction>

<cftry>
<cfscript>

cfclient.file.write("a.txt","hello");
cfclient.file.upload("a.txt","http://<Your-CF-Server>:8500/fileUpload/files/upload.cfm",uploadSuccess,uploadError);

</cfscript> 
<cfcatch type="Any" >
<cfset alert("exception " & cfcatch.message)>
</cfcatch>
</cftry>

</cfclient>

Downloading a file to the device

<cfclientsettings enabledeviceapi="true" enabledebuglog="true">
<cfclient>
<cffunction name="uploadSuccess" >
<cfargument name="obj" >
<cfset alert("downloaded "+obj)>
<cfset alert(cfclient.file.read('b.txt'))>
</cffunction>
<cffunction name="uploadError" >
<cfargument name="err" >
<cfset alert(err.code)>
</cffunction>
<cftry>
<cfscript>
cfclient.file.download("http://<Your-CF-Server>:8500/fileUpload/files/b.txt","b.txt",uploadSuccess,
uploadError);
</cfscript>
<cfcatch type="Any" >
<cfset alert("exception " & cfcatch.message)>
</cfcatch>
</cftry>
</cfclient>
<cfclientsettings enabledeviceapi="true" enabledebuglog="true"> <cfclient> <cffunction name="uploadSuccess" > <cfargument name="obj" > <cfset alert("downloaded "+obj)> <cfset alert(cfclient.file.read('b.txt'))> </cffunction> <cffunction name="uploadError" > <cfargument name="err" > <cfset alert(err.code)> </cffunction> <cftry> <cfscript> cfclient.file.download("http://<Your-CF-Server>:8500/fileUpload/files/b.txt","b.txt",uploadSuccess, uploadError); </cfscript> <cfcatch type="Any" > <cfset alert("exception " & cfcatch.message)> </cfcatch> </cftry> </cfclient>
<cfclientsettings enabledeviceapi="true" enabledebuglog="true">
<cfclient>
<cffunction name="uploadSuccess" >
<cfargument name="obj" >

<cfset alert("downloaded "+obj)>
<cfset alert(cfclient.file.read('b.txt'))>
</cffunction>

<cffunction name="uploadError" >
<cfargument name="err" >

<cfset alert(err.code)>

</cffunction>

<cftry>
<cfscript>

cfclient.file.download("http://<Your-CF-Server>:8500/fileUpload/files/b.txt","b.txt",uploadSuccess,
uploadError);

</cfscript> 
<cfcatch type="Any" >
<cfset alert("exception " & cfcatch.message)>
</cfcatch>
</cftry>

</cfclient>

Creating a directory

<cfclientsettings enableDeviceAPI=true>
<cfclient>
<cfset cfclient.file.createDirectory('MyDir')>
</cfclient>
<cfclientsettings enableDeviceAPI=true> <cfclient> <cfset cfclient.file.createDirectory('MyDir')> </cfclient>
<cfclientsettings enableDeviceAPI=true>
<cfclient>
 
<cfset cfclient.file.createDirectory('MyDir')>
 
</cfclient>

Removing a directory

<cfclientsettings enableDeviceAPI=true>
<cfclient>
<cfif cfclient.file.directoryExists('MyDir')>
<cfset cfclient.file.removeDirectory('MyDir',true)>
</cfif>
</cfclient>
<cfclientsettings enableDeviceAPI=true> <cfclient> <cfif cfclient.file.directoryExists('MyDir')> <cfset cfclient.file.removeDirectory('MyDir',true)> </cfif> </cfclient>
<cfclientsettings enableDeviceAPI=true>
<cfclient>
 
<cfif cfclient.file.directoryExists('MyDir')>
<cfset cfclient.file.removeDirectory('MyDir',true)>
</cfif>
 
</cfclient>

Getting the working directory

<cfclientsettings enableDeviceAPI=true>
<cfclient>
<cfset cwd = cfclient.file.getWorkingDirectory()>
</cfclient>
<cfclientsettings enableDeviceAPI=true> <cfclient> <cfset cwd = cfclient.file.getWorkingDirectory()> </cfclient>
<cfclientsettings enableDeviceAPI=true>
<cfclient>
 
<cfset cwd = cfclient.file.getWorkingDirectory()>
 
</cfclient>

Copying a directory

<cfclientsettings enableDeviceAPI=true>
<cfclient>
<cfif cfclient.file.directoryExists('dir1')>
<cfset cfclient.file.removeDirectory('dir1',true)>
</cfif>
<cfset dir1Obj=cfclient.file.createDirectory('dir1')>
<cfif cfclient.file.directoryExists('dir2')>
<cfset cfclient.file.removeDirectory('dir2',true)>
</cfif>
<cfset dir2Obj=cfclient.file.createDirectory('dir2')>
<cfset cfclient.file.copyDirectory(dir2Obj.fullPath,dir1Obj.fullPath)>
</cfclient>
<cfclientsettings enableDeviceAPI=true> <cfclient> <cfif cfclient.file.directoryExists('dir1')> <cfset cfclient.file.removeDirectory('dir1',true)> </cfif> <cfset dir1Obj=cfclient.file.createDirectory('dir1')> <cfif cfclient.file.directoryExists('dir2')> <cfset cfclient.file.removeDirectory('dir2',true)> </cfif> <cfset dir2Obj=cfclient.file.createDirectory('dir2')> <cfset cfclient.file.copyDirectory(dir2Obj.fullPath,dir1Obj.fullPath)> </cfclient>
<cfclientsettings enableDeviceAPI=true>
<cfclient>
 
<cfif cfclient.file.directoryExists('dir1')>
<cfset cfclient.file.removeDirectory('dir1',true)>
</cfif>

<cfset dir1Obj=cfclient.file.createDirectory('dir1')>


<cfif cfclient.file.directoryExists('dir2')>
<cfset cfclient.file.removeDirectory('dir2',true)>
</cfif>


<cfset dir2Obj=cfclient.file.createDirectory('dir2')>

<cfset cfclient.file.copyDirectory(dir2Obj.fullPath,dir1Obj.fullPath)>

 
</cfclient>

Reading a file

<cfclientsettings enableDeviceAPI=true>
<cfclient>
<cfset fileContent = cfclient.file.read('MyFile.txt')>
</cfclient>
<cfclientsettings enableDeviceAPI=true> <cfclient> <cfset fileContent = cfclient.file.read('MyFile.txt')> </cfclient>
<cfclientsettings enableDeviceAPI=true>
<cfclient>
 
<cfset fileContent = cfclient.file.read('MyFile.txt')>
</cfclient>

Reading a file as a Base64 text

<cfclientsettings enableDeviceAPI=true>
<cfclient>
<cfset opt = cfclient.camera.getOptions()>
<cfset res = cfclient.camera.getPicture(opt,false)>
<cfset imgStr=cfclient.file.readAsBase64(res)>
</cfclient>
<cfclientsettings enableDeviceAPI=true> <cfclient> <cfset opt = cfclient.camera.getOptions()> <cfset res = cfclient.camera.getPicture(opt,false)> <cfset imgStr=cfclient.file.readAsBase64(res)> </cfclient>
<cfclientsettings enableDeviceAPI=true>
<cfclient>
 
<cfset opt = cfclient.camera.getOptions()>
<cfset res = cfclient.camera.getPicture(opt,false)>
<cfset imgStr=cfclient.file.readAsBase64(res)>
 
</cfclient>

Get help faster and easier

New user?