Skip to content

Instantly share code, notes, and snippets.

@roulupen
Created January 7, 2015 15:28
Show Gist options
  • Save roulupen/78908dc66ec1d01ce6f8 to your computer and use it in GitHub Desktop.
Save roulupen/78908dc66ec1d01ce6f8 to your computer and use it in GitHub Desktop.
<cffunction name="importLeadList" description="Import lead List" access="remote" output="false" returnformat="JSON">
<cfargument name="qqfile" type="string" required="true" hint="upload file" />
<cfscript>
var local = structNew();
local.response = structNew();
local.requestData = GetHttpRequestData(); //Get the request headers and body
local.uploadedFileInfo = structNew();
local.imageRecordInsertStatus = true;
local.imageUploadDirectory = "#application.baseDir#\LeadImportFiles\";
</cfscript>
<cftry>
<cfscript>
//If Specified directory doesn't exists then create directory and check again whether it is created or not. If it is not created then throw error.
if(! directoryExists(local.imageUploadDirectory)) {
try {
directoryCreate(local.imageUploadDirectory);
} catch(Any e) {
if(! directoryExists(local.imageUploadDirectory)) {
local.response['success'] = false;
local.response['type'] = 'form';
local.response['reason'] = "Specified destination directory doesn't exists.";
return local.response;
}
}
}
//Extracting file extension from file name
local.fileExtensionWithDot = right(arguments.qqfile, (find(".", reverse(arguments.qqfile))));
</cfscript>
<!--- check if XHR data exists --->
<cfif len(local.requestData.content) GT 0>
<cfset local.response = UploadFileXhr(arguments.qqfile, local.requestData.content)>
<!--- write the contents of the http request to a file. The filename is passed with the qqfile variable --->
<cffile action="write" file="#local.imageUploadDirectory#\#createUUID()##local.fileExtensionWithDot#" output="#arguments.content#" />
<cfset local.uploadedFileInfo['serverFileName'] = "#createUUID()##local.fileExtensionWithDot#" />
<cfset local.uploadedFileInfo['serverFilePath'] = "#local.imageUploadDirectory#\#createUUID()##local.fileExtensionWithDot#" />
<cfelse><!--- no XHR data so process this as standard form submission --->
<!--- upload the file --->
<cffile action="upload" fileField="form.qqfile" destination="#local.imageUploadDirectory#\#createUUID()##local.fileExtensionWithDot#" nameConflict="makeunique" />
<cfset local.uploadedFileInfo['serverFileName'] = file.serverFile />
<cfset local.uploadedFileInfo['serverFilePath'] = "#file.serverDirectory#\#file.serverFile#" />
</cfif>
<cfscript>
//Set response structure
local.response['success'] = true;
local.response['type'] = 'form';
local.response['fileName'] = local.uploadedFileInfo['serverFileName'];
</cfscript>
<cfcatch>
<cfscript>
//Set response structure
local.response['success'] = false;
local.response['type'] = 'form';
local.response['reason'] = cfcatch.Message;
//Log the error details
</cfscript>
</cfcatch>
</cftry>
<cfreturn local.response>
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment