Link to home
Start Free TrialLog in
Avatar of Eric Bourland
Eric BourlandFlag for United States of America

asked on

How to use CFCONTENT to deliver images and documents from a folder outside web root, to a web browser?

ColdFusion 8
MS SQL Server 2005
Windows IIS 6

This question relates to the question with which gdemaria and myselfrandhawa helped me:

https://www.experts-exchange.com/questions/25374756/A-safer-way-to-use-ColdFusion-CFFILE-to-upload-files-to-a-folder-on-a-web-site.html

My question in brief: How can I use CFCONTENT to deliver images and documents from a folder outside web root, to a web browser?

In detail:

Take, for example, this file, located outside of web root:

c:\upload\tpacu.com\iStock_000007799600XSmall.jpg

I want to display it on this page:

http://www.tpacu.com/index.cfm?PageID=3

I cannot just put this code in place to display the image (I tried it):                
         
         <cfcontent
         file = "c:\upload\tpacu.com\iStock_000007799600XSmall.jpg"
         variable = "variable name"
          type = "text/html"
          deleteFile = "no">

What should I do?

I've been reading up on CFCONTENT, in the Forta CF8 book and in various CF web page tutorials. I understand that CFCONTENT tells ColdFusion to send, to a web browser, MIME type text/HTML ... or any MIME type I want.

I am not clear how to use the CFCONTENT tag. I have followed the examples on this page:

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7c82.html

but none give me exactly what I want to do. The CFCONTENT examples in Forta don't apply. This leads me to think that I misunderstand the CFCONTENT tag, or that I need some other mechanism to display, on a web page, content that exists outside web root.

***(Why does the content exist in a folder outside web root? Security reasons. The content is uploaded using CFFILE. I do not want to upload files, using CFFILE, to a folder inside web root.)

Thank you for any advice.

Eric
Avatar of azadisaryev
azadisaryev
Flag of Hong Kong image

this adobe technote explains it pretty well: http://go.adobe.com/kb/ts_kb405330_en-us

for file types other than jpeg images, just change the TYPE attribute of <cfcontent> tag to correct mime type.

another addition to the technote i may make is that you can easily add a query string to src attribute of the <img> tag to identify specific file to stream based on its name or some other identifier (i.e. db record id):

<img src="image.cfm?imgid=XXXX">

same goes for other file types. of course, you would not use <img> tag to serve for example pdf documents; for this you might use something like <a href="pdf.cfm?docid=YYYYY" target="_blank">, which will serve the pdf in a new tab/window or prompt the user to download it (depends on user's browser settings)

hth

Azadi
Avatar of Eric Bourland

ASKER

Cool. I'll give this a go. Thank you Azadi!

Peace. EB
Hi, Azadi,

Hmmm. Two things are going on to complicate matters.

* The first thing is, I need to begin to load images and documents as a part of each database record, so that, as you mentioned, I can refer to images and documents according to a proper database record ID. Something like <img src="image.cfm?imgid=XXXX"> would be perfect, and that is a great idea. But I will work on this later. It is a separate task.

* The second thing is, I followed carefully the instructions in the Adobe technote, but my image does not appear. I have tried to specify an image outside of web root, and also an image within web root, but neither works. Is there a setting in ColdFusion administrator that I need to activate, in order to use CFCONTENT?

I don't get any error; the image simply does not appear. What am I missing? I attach my files below: index_test.cfm and image.cfm, both of which are in web root "/".

Is #ExpandPath a variable? I don't think it is, since it is not referenced in any query and there is no closing #.

Thanks for your advice. Hope you are well.

Eric
image.cfm:
 <cfcontent type = "image/jpeg" file = "#ExpandPath('c:\upload\tpacu.com\iStock_000007799600XSmall.jpg')" deleteFile = "No" reset="yes">

index_test.cfm:
<cfinclude template="/tpacu_header.cfm" />
<cfparam name = "url.PageId" default = "1" />
<cfinclude template="/tpacu_dropdown_menus.cfm" />
 
<cfquery name="getTPACUContent" datasource="ebwebwork" dbname="ebwebwork">
SELECT PageID, PageTitle, PageContentLeft, PageContentRight
FROM tpacu
WHERE PageID=#URL.PageID#
</cfquery>
 
 <!-- begin row_content -->
<div id="row_content">
 
 
 
 <img href="image.cfm" />
 
 


 
 
<!-- end row_content -->
</div>
 
 
 
 


<cfinclude template="/tpacu_footer0.cfm" />

<cfinclude template="/tpacu_footer1.cfm" />

Open in new window

change
file = "#ExpandPath('c:\upload\tpacu.com\iStock_000007799600XSmall.jpg')"
to just
file = "c:\upload\tpacu.com\iStock_000007799600XSmall.jpg"

first, you have a syntax error there - missing closing #. if you used firebug to check the response from image.cfm, you would have probably seen a cf error message.

secondly, expandpath() is a cf function that translates a relative path to an absolute path. since you are already providing full absolute path there, you do not need to use expandpath() at all.

Azadi
Got it. I'm working on it.

Hmmm, I did try just using the simple, direct path to the image. Will try again.
Azadi,

Thank you for explaining about function expandpath().

Still no luck with CFCONTENT. I believe I have used the correct syntax. I think there is something else going on.

The image c:\upload\tpacu.com\iStock_000007799600XSmall.jpg (and this path and filename are correct) should show up here: http://www.tpacu.com/index_test.cfm

I am checking in ColdFusion administrator to see if there is an option to activate or deactivate CFCONTENT.

Eric
image.cfm:
 <cfcontent type = "image/jpeg" file = "c:\upload\tpacu.com\iStock_000007799600XSmall.jpg" deleteFile = "No" reset="yes">


index_test.cfm:

<cfinclude template="/tpacu_header.cfm" />
<cfparam name = "url.PageId" default = "1" />
<cfinclude template="/tpacu_dropdown_menus.cfm" />
 
<cfquery name="getTPACUContent" datasource="ebwebwork" dbname="ebwebwork">
SELECT PageID, PageTitle, PageContentLeft, PageContentRight
FROM tpacu
WHERE PageID=#URL.PageID#
</cfquery>
 
 <!-- begin row_content -->
<div id="row_content">
 
 
 
 <img href="image.cfm">
  
 
<!-- end row_content -->
</div>
 

<cfinclude template="/tpacu_footer0.cfm" />

<cfinclude template="/tpacu_footer1.cfm" />

Open in new window

if you go to http://www.tpacu.com/image.cfm you will see the image (or at least i do). but on your index page it does not show up. so something else is amiss here...

looking deeper into it now...

Azadi
I don't see any place to activate or deactivate the CFCONTENT file. I wonder if I should use the Solution A at http://kb2.adobe.com/cps/405/kb405330.html. That's a less elegant solution.
ASKER CERTIFIED SOLUTION
Avatar of azadisaryev
azadisaryev
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ahhh. OK, one sec.
Great success. Thank you again Azadi.
It works:

http://www.tpacu.com/index_test.cfm

=) Thanks Azadi.

Next I am going to work on The first thing is loading images and documents as parts of a database record, so that, as you mentioned, I can refer to images and documents according to a proper database record ID.

I really appreciate your help and time. Have a good evening.

Eric
glad i could help!
(and it is morning here in Hong Kong...)

Azadi
that did not work for me