Uploading and Renaming Image Files

Discussion in 'Programming' started by forumposters, Jan 30, 2007.

  1. #1
    Can anyone show me some code using CFFILE to upload and rename an image file? One problem that I've ran into is that I can't see how to detect and store in a variable the extension of the image (ie .jpg, .gif, or .png )
     
    forumposters, Jan 30, 2007 IP
  2. Kerunai

    Kerunai Active Member

    Messages:
    649
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #2
    yeah, the one like jonathan ledger put on his blog, very neat and promote customer's activityt
     
    Kerunai, Jan 30, 2007 IP
  3. forumposters

    forumposters Peon

    Messages:
    270
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'd also like to know about how you could use an iframe to upload multiple files and display a thumbnail of each image without refreshing the page. Anyone know how that might be done? Can it be done with just Coldfusion and perhaps Flash?
     
    forumposters, Feb 7, 2007 IP
  4. forumposters

    forumposters Peon

    Messages:
    270
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I've found a script that does almost exactly what I need but it uses PHP - I know nothing about PHP. Anyone here want to help me convert the PHP to Coldfusion? It's not a whole lot of code. Here's a demo and the source code is below the demo:

    http://www.air4web.com/files/upload/
     
    forumposters, Feb 8, 2007 IP
  5. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Hi - I've been out for a bit, I'd like to help out if you still need the help
     
    datropics, Feb 19, 2007 IP
  6. OneWebAve

    OneWebAve Peon

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I'm doing both of those in a current application.

    Here's the upload and rename code (I'm actually moving it at the same time I rename it...)

    			<cffile 
    				action="UPLOAD" 		
    				destination="#application.root_coder_directory#\sites\#replace(session.user_site_name, " ", "_", "ALL")#\patients\#session.patient_id#\" 
    				filefield="client_file" 
    				nameconflict="MAKEUNIQUE"
    				accept="image/jpg, image/gif, image/tif, image/tiff, image/png, image/pjpeg"> 
    
    			<cffile 
    				action="RENAME" 
    				source="#application.root_coder_directory#\sites\#replace(session.user_site_name, " ", "_", "ALL")#\patients\#session.patient_id#\#cffile.serverFile#" 
    				destination="#application.root_coder_directory#\sites\#replace(session.user_site_name, " ", "_", "ALL")#\patients\#session.patient_id#\#Replace(cffile.serverFile, " ", "_", "ALL")#">
    
    Code (markup):
    Note the use of session variables and such to control the name - this is not needed for simple programs.

    To display thumbnails, you can use ImageMagick to (1) cffile upload images then (2) cfexecute and convert the images and then (3) have an iframe or scrollable DIV tag to display the images. Here's my code:

    Upload and convert size (I'm downsizing but not too much - 1000pixels wide or so.)

    		<cfelseif parameterExists(form.client_file)>
    			<cftry>
    			<cffile 
    				action="UPLOAD" 		
    				destination="#application.root_coder_directory#\sites\#replace(session.user_site_name, " ", "_", "ALL")#\patients\#session.patient_id#\" 
    				filefield="client_file" 
    				nameconflict="MAKEUNIQUE"
    				accept="image/jpg, image/gif, image/tif, image/tiff, image/png, image/pjpeg"> 
    
    			<cffile 
    				action="RENAME" 
    				source="#application.root_coder_directory#\sites\#replace(session.user_site_name, " ", "_", "ALL")#\patients\#session.patient_id#\#cffile.serverFile#" 
    				destination="#application.root_coder_directory#\sites\#replace(session.user_site_name, " ", "_", "ALL")#\patients\#session.patient_id#\#Replace(cffile.serverFile, " ", "_", "ALL")#">
    			<cf_audit action="Patient scanned record saved">
    			<cfcatch>
    				<cf_header>
    					<h2 align="center" style="color:red">Error - invalid file</h2>
    					<div align="center"><b>The file you uploaded was not accepted by the server.</b>  Only images may be uploaded. Please go back and try again.</div>
    				<cf_footer>
    			
    			</cfcatch>
    			</cftry>		
    
    		</cfif>
    		<cfif ParameterExists(cffile.CONTENTSUBTYPE) and cffile.CONTENTSUBTYPE EQ "tiff">
    			<cfset variables.arguments = '/c convert -resize 800x1200">" "#application.root_coder_directory#\sites\#replace(session.user_site_name, " ", "_", "ALL")#\patients\#session.patient_id#\#Replace(cffile.serverFile, " ", "_", "ALL")#" "#application.root_coder_directory#\sites\#replace(session.user_site_name, " ", "_", "ALL")#\patients\#session.patient_id#\#Replace(cffile.serverFileName, " ", "_", "ALL")#.jpg"'>
    			<cfexecute 
    				name="cmd.exe" 
    				arguments="#variables.arguments#"
    				variable="converter"></cfexecute> 
    			
    			<!--- you have to give the page time to load or it fails to locate the files it has just created --->
    			<cfparam name="attributes.interval" default="5"/>
    			<cfscript>
    			thread = createObject("java", "java.lang.Thread");
    			thread.sleep(javaCast("long", 1000*attributes.interval));
    			</cfscript>
    			<cfif fileExists("#application.root_coder_directory#\sites\#replace(session.user_site_name, " ", "_", "ALL")#\patients\#session.patient_id#\#Replace(cffile.serverFile, " ", "_", "ALL")#")>
    <!--- 				<cffile 
    					action="DELETE" 
    					file="#application.root_coder_directory#\sites\#replace(session.user_site_name, " ", "_", "ALL")#\patients\#session.patient_id#\#Replace(cffile.serverFile, " ", "_", "ALL")#">
     --->			</cfif>
    
    		</cfif>		
    
    
    
    Code (markup):
    Note that I am working on code to delete the original file because I am converting them from multi-page TIFF files to individual JPG files. You probably won't want to delete them in your case!


    Embed this code in your "display" page:
    		<tr>
    			<td colspan="4" valign="top">
    			<cfoutput>
    			<div align="center" style="width:100%; height:250px; overflow:auto;">
    				<iframe align="middle" frameborder="1" height="250" width="100%" src="document_edit_imageHelper.cfm?doc_id=#url.doc_id#" scrolling="Auto" style="background-image: url(images/animclock.gif);background-repeat:no-repeat"></iframe>
    			</div>
    			</cfoutput>
    
    			</td>
    		</tr>
    
    Code (markup):
    Then this is the code the displays the images. Notice I was outputting them in PDF format, but that was slow as hell so I removed that for now.

    <cfsetting showDebugOutput="no">
    
    <cfdirectory 
    	action="LIST" 
    	directory="#application.root_coder_directory#\sites\#replace(session.user_site_name, " ", "_", "ALL")#\patients\#url.doc_id#" 
    	name="q_GetFiles">
    
    <cfquery datasource="#application.dsn#" name="q_GetRecord">
    	SELECT 	Patient_record
    	FROM 	Patients
    	WHERE 	patient_id = '#url.doc_id#'
    	AND 	patient_record <> ''
    </cfquery>
    		
    <!--- <cfdocument 
    		format="PDF"
    		encryption="128-bit"
    		permissions="AllowScreenReaders">
     --->
    
    		<cfif q_GetRecord.RecordCount GT 0>
    			<cfoutput>#replace(q_GetRecord.Patient_record, chr(13), "<br>", "ALL")#</cfoutput>
    		</cfif>
    
    		<cfif ParameterExists(session.patient_record) and session.patient_record NEQ "">
    			<cfoutput><div style="font-size:12px;font-family:verdana">#Session.patient_record#</div></cfoutput><br><br>
    		</cfif>
    		
    		<cfif Q_GetFiles.RecordCount GT 0>
    			<cfoutput query="Q_GetFiles">
    				<cfif NOT findNoCase(".tif", Q_GetFiles.name) and NOT FindNoCase(".tiff", Q_GetFiles.name)>
    					<img src="#application.webroot_coder_directory#/sites/#replace(session.user_site_name, " ", "_", "ALL")#/patients/#url.doc_id#/#Q_GetFiles.name#" border="1" <cfif FindNoCase("imageData", Q_GetFiles.name)>width="800"</cfif>><br><br>
    <hr width="100%" color="red" size="2">
    &nbsp;
    
    
    <!--- 	 				<cfif IsUserInRole("SuperAdmin")>				#application.webroot_coder_directory#/sites/#replace(session.user_site_name, " ", "_", "ALL")#/patients/#url.doc_id#/#Q_GetFiles.name#
    					</cfif>
     --->
     				</cfif> 
    			</cfoutput>
    		</cfif>
    		<cfif q_GetRecord.RecordCount EQ 0 AND Q_GetFiles.RecordCount EQ 0 and (NOT ParameterExists(session.patient_record) OR session.patient_record EQ "")>
    	<div style="font-size:12px;font-family:verdana">
    		<br>
    		<b>This patient has no records assigned to them.</b>
    	</div>
    		</cfif>
    		<br>
    		<br>
    		<div style="font-size:9px;font-family:verdana"><i>The information contained in this page is protected under Privacy and Health Insurance Portability and Accountability Acts. Improper disclosure or use (including: saving local copies of any kind, copying, printing or transmittal) of this Protected Health Information without a signed release is prohibited and can result in fines and imprisonment.</i></div><br>
    <!--- </cfdocument> --->
    
    
    Code (markup):
    I have some permissions wrapped around it, but I am sure you can figure it out.

    Good luck!
     
    OneWebAve, Mar 4, 2007 IP