uploading multiple files using coldfusion

cpwilson
cpwilson used Ask the Experts™
on
Is there a way to upload multiple files (principally images) and insert them into a data table using coldfusion, similar to what appears to b capable in Wordpress
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
erikTsomikSystem Architect, CF programmer

Commented:
Yes there is a way just create a form with multiple upload fields <input type="file"

and once you click submit loop through the form fields and insert into the database.

also depending on version of coldfsuion you are using latest one can do something like this

https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-f/cffile-action-uploadall.html

or this

https://www.bennadel.com/blog/1117-ask-ben-uploading-multiple-files-using-coldfusion.htm
Most Valuable Expert 2015
Commented:
If you mean this plugin most of what it does is achieved with HTML5 and javascript, rather than any server side language.  You can use an HTML5 file input for selecting multiple files. Then use <cffile action=uploadAll> and <cfquery> for saving the files on the server and to your database.  However, the rest of the bells and whistles (like progress bars, etc... ) is javascript and will require some sort of JS plugin.

If all you're looking for is the file upload, here's a working example. Adjust the allowed file types and extensions as needed.

Upload Form
<!---
	Upload files 
--->
<cfif structKeyExists(form, "myFile")>
	<!--- upload files outside web root --->
	<cffile action="uploadall"
		destination="c:\somePath\outside\webroot"
        nameconflict="MakeUnique"
		accept="image/png,image/jpeg"               
        strict="true"
		result="results"
		continueOnError="true"
		errors="errors"
		allowedextensions=".png,.jpeg,.jpg"
		>
		
	<!--- 
		If successful, store files in database 
	--->
	<cfif !errors.len()>
		<cftransaction>
			<cfloop array="#results#" index="props">
				<cfquery datasource="YourDatasourceNameHere">
					INSERT INTO YourTable 
					(
						originalFileName, savedFileName, fileExt, contentType, fileSize 
					)
					VALUES 
					(
						<cfqueryparam value="#props.AttemptedServerFile#" cfsqltype="cf_sql_varchar">
						, <cfqueryparam value="#props.ServerFile#" cfsqltype="cf_sql_varchar">
						, <cfqueryparam value="#props.ServerFileExt#" cfsqltype="cf_sql_varchar">
						, <cfqueryparam value="#props.ContentType#/#props.ContentSubType#" cfsqltype="cf_sql_varchar">
						, <cfqueryparam value="#props.FileSize#" cfsqltype="cf_sql_bigint">
					)
				</cfquery>
			</cfloop>
		</cftransaction>
	<cfelse>
		<!--- do error handling here (delete uploaded files, etc.) --->
		<cfdump var="#errors#" label="Upload Errors">
		<cfdump var="#results#" label="Successful uploads">
	</cfif>
</cfif>

<!---
	Upload form
--->
<form method="post" enctype="multipart/form-data"> 
	<input type="file" name="myFile" multiple>
	<input type="submit">
</form>

Open in new window

DDL (SQL Server)

CREATE TABLE YourTable
(
	id int identity
	, originalFileName varchar(500)
	, savedFileName varchar(500)
	, contentType varchar(250)
	, fileSize bigint
	, fileExt varchar(50)
)

Open in new window

Author

Commented:
This worked first time trying. _agx you rock. Thanks so much
Most Valuable Expert 2015

Commented:
Glad it helped!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial