Forums

This topic is locked

Show Image Based Upon File Extension

Posted 02 Feb 2006 05:58:08
1
has voted
02 Feb 2006 05:58:08 Jeremy Conn posted:
OK, I did a search on Google, Yahoo!, and DMXZone and can't find anything that does what I am looking for. Yeehaw, a challenge for everyone looking for some ASP excitement. <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

Working in: ASP/VBScript, Access

This is what I want to do:
<b>When displaying records from my DB, I want to have a particular image show based upon the file extension of the filename stored in that record.</b>

I am using a normal recordset and repeat region, and simply displaying the record name and the link (via the filename) to the document that was uploaded. I want the Word document icon to display if it is a .doc, a PDF icon to display if it is a .pdf, etc. I have all the icons created, but have no way to recognize the file extension in my repeat region.

My guess as to how this would work theoretically is something like this... but I don't know how to have this code look only for the file extension. Any takers?

&lt;% If rsDownloads.Fields.Item("File".Value = "doc" Then %&gt;
<b>&lt;img src="images/icons/doc.gif"&gt;</b> &lt;%=(rsDownloads.Fields.Item("File".Value)%&gt;
&lt;% End If %&gt;
&lt;% If rsDownloads.Fields.Item("File".Value = "pdf" Then %&gt;
<b>&lt;img src="images/icons/pdf.gif"&gt;</b> &lt;%=(rsDownloads.Fields.Item("File".Value)%&gt;
&lt;% End If %&gt;


<b>Jeremy Conn</b>
www.conncreativemedia.com
DWMX2004 | ASP/VB | Access/SQL2000

Edited by - connman21 on 02 Feb 2006 06:00:40

Edited by - connman21 on 02 Feb 2006 06:01:07

Edited by - connman21 on 02 Feb 2006 06:03:05

Replies

Replied 03 Feb 2006 00:23:49
03 Feb 2006 00:23:49 Dave Thomas replied:
i used to use some code virtually identical to what you wrote for displaying a tick or cross depending on a checkbox, it worked great, so as long as the db column holds the value i can't see no reason why your display won't work.
the code i had didn't use the second value line. so try.

so here's how i got it to work

created a column in the db, to hold a 3 letter value for the extensions, better ways to do this with a relationship, but just for test purposes i kept it simple as can be. field created is called "file_ext"
then i simply added a column to the table to test the results.
this is where the code goes.

<pre id=code><font face=courier size=2 id=code>
&lt;% If rsDownloads.Fields.Item("file_ext".Value = "DOC" Then %&gt;
&lt;img src="../files/doc.gif"&gt;
&lt;% End If %&gt;
&lt;% If rsDownloads.Fields.Item("file_ext".Value = "PDF" Then %&gt;
&lt;img src="../files/pdf.gif"&gt;
&lt;% End If %&gt;
&lt;% If rsDownloads.Fields.Item("file_ext".Value = "TXT" Then %&gt;
&lt;img src="../files/txt.gif"&gt;
&lt;% End If %&gt;
</font id=code></pre id=code>

edit: now works <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

u can see a screenie below.

regards

Dave Thomas
<b>DMX Zone Forums Manager</b>
Tip: Use Google or our own Search function to find answers before asking. You'd be surprised what's already written <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>
Replied 05 Feb 2006 05:07:36
05 Feb 2006 05:07:36 Jeremy Conn replied:
Thanks for the reply, Dave, but I think I found what I was looking for.
This code produced the result that I want... take a look:

&lt;%
dim strFileName, str3, strExt
'Getting the filename from our database and setting the string contents.
strFileName = UCase(varFile)
'Getting the number of character contained within the filename.
intHowLong = Len(strFileName)
'Subtracting the number of characters then adding 3. This is effectively removing the filename and leaving only the extension.
str3 = intHowLong - intHowLong + 3
'Isolating the extension (the last three characters of strFileName) from the filename.
strExt = Right(strFilename, str3)
%&gt;

The result of this code basically results in the 3-letter extension of the filename. If it is a PDF, the result will be pdf - a Word document will be doc.
So, I place this code just inside my repeat region, and then replace the name of my icon gif with the result of the code and I get a dynamic icon.
NOTE: For this to work, you must create icons that are named the exact 3-letter names as the extensions they represent.

<b>NEW IMAGE CODE WITHIN REPEAT REGION:</b>
&lt;img src="images/icons/&lt;%=strExt %&gt;.gif" width="18" height="18" align="absmiddle"&gt;

Works like a charm.

<b>Jeremy Conn</b>
www.conncreativemedia.com
DWMX2004 | ASP/VB | Access/SQL2000
Replied 08 Feb 2006 00:08:47
08 Feb 2006 00:08:47 Dave Thomas replied:
i actually got it to work in access as you first wanted.

all i did was create a field in the db to hold a 3 letter value, ie: doc, pdf, txt etc..

and then run the code i mentioned. works fine.

glad you found a way <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

files are here if you want a peek, just unrar them into your wwwroot.
www.bluecoder.co.uk/dmx/ASPTests.rar

Screenie mentioned above

<img src="www.bluecoder.co.uk/dmx/file_extension.jpg" border=0>

regards

Dave Thomas
<b>DMX Zone Forums Manager</b>
Tip: Use Google or our own Search function to find answers before asking. You'd be surprised what's already written <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>
Replied 30 Apr 2007 13:36:36
30 Apr 2007 13:36:36 Tim Brown replied:
This is more compact and can work with any length extension. It is also more flexible as the file name can be placed in the html from the database rather than calling it elsewhere and it can be repeated as many times as you like down the page.

&lt;%
function icon(varFile)
dim strFileName
'Split the file down by . so that we can extract the extension of any length without any problems
strFileName = split(UCase(varFile),"."
'Grab the last variable in the array, which is the extension
icon = strFileName(ubound(strFileName)) & ".GIF"
end function
%&gt;

&lt;!-- Replace file.extension with your database file name --&gt;
&lt;img src="images/&lt;%=icon("file.extension"%&gt;" /&gt;

Reply to this topic