Solved

CF - File upload then available link

Posted on 2010-08-16
23
459 Views
Last Modified: 2013-12-16
I have to redesign the company intranet and they want the capability for departments to upload a Word doc for example and then have it available for others to download.  I have upload code but I have no idea how to have the uploaded file become a link.  Any help is appreciated.


<cfif isdefined("form.submit_upload")>
<cffile action="UPLOAD" filefield="file_path" destination="C:\inetpub\wwwroot" nameconflict="MAKEUNIQUE">
File Uploaded!
</cfif> 

<form action="uploadfile.cfm" method="POST" name="frmupload" enctype="multipart/form-data">
<input type="file" name="file_path">
<input type="submit" name="submit_upload" value="upload">
</form>

Open in new window

0
Comment
Question by:JohnMac328
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
  • 2
23 Comments
 
LVL 29

Expert Comment

by:chilternPC
ID: 33448913
I would buy a package off the shelf and integrate it.  you are just re-inventiing the wheel otherwise
Open source CF library managers such as http://www.opensourcecf.com/cffm/
is free and ready to go..
0
 

Author Comment

by:JohnMac328
ID: 33448953
Doubtful they will spring for any COTS package.  I really need some code to show how that is accomplished.
0
 
LVL 29

Expert Comment

by:chilternPC
ID: 33449128
well, I'm guessing even CF at the adobe site will have pre-built file managers.
what about over-writing, deleting, renaming of files uploaded?
what about updating the file?  will you be able to have access rights to thefiles etc...
what about undelete or backup?

in dreamweaver you can get access to CF extension on the adobe site such this "file upload"

http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&loc=en_us&extid=1000745

then they just add that functionality to DW and you can use it as easy as 123
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:JohnMac328
ID: 33449218
It does upload the file like my example does but how does a person download the file after it has been uploaded?.  Does CF have a function like VB where you can browse the contents of a folder?  That might be an option.
0
 

Author Comment

by:JohnMac328
ID: 33449411
Here is something I found that may get close.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title><cfoutput>#Replace(cgi.script_name, "index.cfm", "")#</cfoutput></title>
<style type="text/css">
	body,td { font-family: verdana; padding: 2px 0px 2px 0px; color:silver;  } 
	th { border-bottom: 1px solid silver;text-align:left;margin-bottom: 2px; color:black; }
	a.sort { text-decoration: none;	color: silver; }
	a:hover { background-color: blue; color: white;	}
	p { font-size: x-small; }
</style>
</head>
<body>
<cfparam name="url.sort" default="datelastmodified desc">
<cfdirectory directory="#ExpandPath("./")#" action="list" name="dir" sort="#url.sort#">

<table width="100%" cellpadding="0" cellspacing="0">
	<tr>
		<th>Name <a href="?sort=name" class="sort" title="Sort By Name">¿</a></th>
		<th>Size (bytes) <a href="?sort=size" class="sort" title="Sort By Size">¿</a></th>
		<th>Last Modified <a href="?sort=datelastmodified+desc" class="sort" title="Sort By Date">¿</a></th>
	</tr>
	<cfoutput query="dir">
	<cfif dir.name IS NOT "index.cfm">
	<tr>
		<td><a href="#dir.name#">#dir.name#</a></td>
		<td>#dir.size#</td>
		<td>#dir.datelastmodified#</td>
	</tr>
	</cfif>
	</cfoutput>
</table>
<p>Directory Browser by <a href="http://www.petefreitag.com/">Pete Freitag</a></p>
</body>
</html>

Open in new window

0
 
LVL 9

Accepted Solution

by:
Shaun McNicholas earned 500 total points
ID: 33454107
You'll need to understand the cfdirectory tag and options and the cffile tag and options. You are using the cffile upload option to upload the file and you can use the same tag to read information about files within a directory as long as you know the directory. I would move the directory where you are storing files to somewhere other than C:\Inetpub\wwwroot - this is the main directory for the web server so it's going to get cluttered with files. You should consider using usernames or something to separate files from different users or some sort of method for saving your files in a more specific and easily understood location.

<!--- Set your directory location and its corresponding url location here --->
<cfset application.documentsDirectory = "C:\Inetpub\wwwroot\#session.user_id#\">
<cfset application.documentsDirUrl = "http://thiswebserverlocation/#session.user_id#/">

<!--- Get Directory Contents --->
<cfdirectory directory="#application.documentsDirectory#" name="documentsDirectory" sort="size ASC, name DESC, datelastmodified">

<!--- This checks for the existence of at least 1 file within the directory and if true loops through the list of files and outputs each filename with a linking url. There are many more options available for reading the type of file - it's size etc... using the cfdirectory and cffile tags but you'll have to look in the reference pdf for more information --->
<cfif IsDefined("documentsDirectory") and documentsDirectory.RecordCount gt 0>
<cfloop query="documentsDirectory">
<cfoutput><a href="#application.documentsDirUrl#/#documentsDirectory.filename#">#documentsDirectory.filename#</a></cfoutput><br />
</cfloop>
</cfif>
0
 

Author Comment

by:JohnMac328
ID: 33454159
Ok that is good info.  First off I am just seeing what is possible.  I will be setting up roles and they will all upload to a different folder based on their role.  Let me try your example to see what it looks like.
0
 

Author Comment

by:JohnMac328
ID: 33454796
I am trying to setup a quick session variable but it gives the "Variable USER_ID is undefined, what do I need short term just to see how the files work?
<CFAPPLICATION NAME="Name"
SESSIONMANAGEMENT="Yes"
SESSIONTIMEOUT="#CreateTimeSpan(0, 0, 20, 0)#">
<CFSET session.name="#user_id#">

Open in new window

0
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 33454888
You have to define the #user_id# since you have it inside pound signs CF is looking for a pre-defined variable - if you just want to use "user_id" as the name of a test user then just use <cfset session.name = "user_id"> Or type someones actual name in there - if you don't already have some kind of authentication system in place that keeps track of who is logging in.
0
 

Author Comment

by:JohnMac328
ID: 33454963
Here is what I have but it is a blank screen.  I have more than one file in the CF_Upload File folder.  I just wanted to see what the files looked like listed and go from there.
<CFSET session.user_id="user_id">
<!--- Set your directory location and its corresponding url location here --->
<cfset application.documentsDirectory = "C:\Inetpub\wwwroot\CF_Upload File\#session.user_id#\">
<cfset application.documentsDirUrl = "http://localhost/CF_Upload%20File/#session.user_id#/">

Open in new window

0
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 33455221
You are telling the application to look in the following directory for files

C:\Inetpob\wwwroot\CF_Upload File\user_id\

And just as a good programming practice you'll save yourself a lot of extra programming issues if you avoid blank spaces when creating files and folders - every system interprets them differently and you'll save yourself lots of extra aggravation for the future if you develop the practice now. Imagine trying to deal with those spaces and converting them back and forth if you decide to store the locations of those files in a database where in some databases you would have to convert it to a #Chr(10)# then back to %20 for the url display and link... gets crazy.

Anyway - are there some files in the directory \user_id\??? Those should get listed in the cfloop
You can always do a cfdump as well to see the results of the actual query created from the cfdirectory command.
try this:

<!--- Get Directory Contents for \CF_Upload File\ --->
<cfdirectory directory="C:\Inetpub\wwwroot\CF_Upload File\" name="documentsDirectory1" sort="size ASC, name DESC, datelastmodified">
<cfdump var="#documentsDirectory1#">

<!--- Get Directory Contents for \user_id\ --->
<cfdirectory directory="C:\Inetpub\wwwroot\CF_Upload File\user_id\" name="documentsDirectory2" sort="size ASC, name DESC, datelastmodified">
<cfdump var="#documentsDirectory2#">
0
 

Author Comment

by:JohnMac328
ID: 33455275
Now this popped up
Element FILENAME is undefined in DOCUMENTSDIRECTORY.  
 
  
The error occurred in C:\Inetpub\wwwroot\CF_Upload File\index.cfm: line 12
 
10 : <cfif IsDefined("documentsDirectory") and documentsDirectory.RecordCount gt 0>
11 : <cfloop query="documentsDirectory">
12 : <cfoutput><a href="#application.documentsDirUrl#/#documentsDirectory.filename#">#documentsDirectory.filename#</a></cfoutput><br />
13 : </cfloop>
14 : </cfif>

 

Open in new window

0
 

Author Comment

by:JohnMac328
ID: 33455352
Here is the whole page with folder name change
<CFSET session.user_id="user_id">
<!--- Set your directory location and its corresponding url location here --->
<cfset application.documentsDirectory = "C:\Inetpub\wwwroot\CF_UploadFile\#session.user_id#\">
<cfset application.documentsDirUrl = "http://localhost/CF_UploadFile/#session.user_id#/">

<!--- Get Directory Contents --->
<cfdirectory directory="#application.documentsDirectory#" name="documentsDirectory" sort="size ASC, name DESC, datelastmodified">

<!--- This checks for the existence of at least 1 file within the directory and if true loops through the list of files and outputs each filename with a linking url. There are many more options available for reading the type of file - it's size etc... using the cfdirectory and cffile tags but you'll have to look in the reference pdf for more information --->
<cfif IsDefined("documentsDirectory") and documentsDirectory.RecordCount gt 0>
<cfloop query="documentsDirectory">
<cfoutput><a href="#application.documentsDirUrl#/#documentsDirectory.filename#">#documentsDirectory.filename#</a></cfoutput><br />
</cfloop>
</cfif>

Open in new window

0
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 33455356
Sorry change the documentsDirectory.filename variable to just documentsDirectory.name
I copied the function from something else I was working on that was pulling the information from a database query... sorry about that.

0
 

Author Comment

by:JohnMac328
ID: 33455395
No problem.  Ok that gets me what I needed to start.  Quick question.  Do you know if they have made a tag that would display the icon by the file, Word for example?
0
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 33455402
If you have issues like that there are two ways to debug things in ColdFusion that I use all the time - you can do the <cfdump var="#variableInQuestion#"> to look at the structure of the data you are trying to reference. Like in this case the documentsDirectory which should be a query with named columns - one of those columns that we are trying to reference in our loop was the filename - if you had a dump to look at you would see the structure of the dump that there is no column named filename, but there is a column called name.

Also you should turn on debugging output and it will give you loads of information about the variables on this page and the queries or functions that generate them...
0
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 33455473
This is how I display file types - I am sure there probably is something in CF to do this automatically but I am a control freak so I like to understand exactly what's happening myself. I even right all my own ajax stuff instead of relying on CF's ajax tags.

    <!--- This gives you the last four characters from the files name in the directory --->
    <cfset document_type=right(documentsDirectory.name,4)>
    <cfif document_type is ".doc"><cfset document_icon="/images/documents_icons/word.gif"><cfset document_title = "Word">
    <cfelseif document_type is ".xls"><cfset document_icon="/images/documents_icons/excel.gif"><cfset document_title = "Excel">
    <cfelseif document_type is ".pdf"><cfset document_icon="/images/documents_icons/pdf.gif"><cfset document_title = "Adobe PDF">
    <cfelseif document_type is ".txt"><cfset document_icon="/images/documents_icons/txt.gif"><cfset document_title = "Text">
    <cfelse><cfset document_icon="/images/documents_icons/other.gif"><cfset document_title = "Other"></cfif>

Then when listing the files --
Of course this assumes that you have those images as gif files in a directory at C:\Inetpub\wwwroot\images\documents_icons\
I just downloaded some cool icons from somewhere on the web and put them in there for frequent use.
<a href="#application.documentsDirUrl#/#documentsDirectory.name#"><img src="#document_icon#" width="16" height="16" align="absbottom" title="#document_title#" border="0" />#documentsDirectory.name#</a>
0
 

Author Closing Comment

by:JohnMac328
ID: 33455496
Great Thanks.  You gave an incredible amount of good info.
0
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 33455545
Here's a function for showing size like you see on your computer

    <cfif documentsDirectory.size lt 1025><cfset document_size = documentsDirectory.size><cfset size_type = "bytes">
    <cfelseif documentsDirectory.size gt 1024 and documentsDirectory.size lt 1048577><cfset document_size = documentsDirectory.size/1024><cfset size_type = "Kb">
    <cfelse><cfset document_size = documentsDirectory.size/1048576><cfset size_type = "Mb">
    </cfif>

And the display:
<a href="#application.documentsDirUrl#/#documentsDirectory.name#"><img src="#document_icon#" width="16" height="16" align="absbottom" title="#document_title#" border="0" />#documentsDirectory.name#</a> #document_size# #size_type#
0
 

Author Comment

by:JohnMac328
ID: 33455561
Awesome - thanks again.
0
 

Author Comment

by:JohnMac328
ID: 33455810
Kind of strange but these two files are different sizes and they display like this

EM-AutoSalesinTurkey080610.gif10.154296875 Kb
RyanReport07122010.doc10.154296875 Kb

Both numbers are the sane (10.154296875 Kb)
0
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 33456171
Put up the whole page's code and I'll look at it - probably you just need to move the document type and size if statements so that those functions occur inside the loop. You probably have that function outside of the loop so it's only giving you the type and size of the first file in the loop.
0
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 33456191
Also when you output the file name etc... change it to this so it shows the file size without all the extra numbers. You probably don't want to see all the extra information.

#NumberFormat(document_size,"___,___")#
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question