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?
<% If rsDownloads.Fields.Item("File".Value = "doc" Then %>
<b><img src="images/icons/doc.gif"></b> <%=(rsDownloads.Fields.Item("File".Value)%>
<% End If %>
<% If rsDownloads.Fields.Item("File".Value = "pdf" Then %>
<b><img src="images/icons/pdf.gif"></b> <%=(rsDownloads.Fields.Item("File".Value)%>
<% End If %>
<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>
<% If rsDownloads.Fields.Item("file_ext".Value = "DOC" Then %>
<img src="../files/doc.gif">
<% End If %>
<% If rsDownloads.Fields.Item("file_ext".Value = "PDF" Then %>
<img src="../files/pdf.gif">
<% End If %>
<% If rsDownloads.Fields.Item("file_ext".Value = "TXT" Then %>
<img src="../files/txt.gif">
<% End If %>
</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>
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>
<% If rsDownloads.Fields.Item("file_ext".Value = "DOC" Then %>
<img src="../files/doc.gif">
<% End If %>
<% If rsDownloads.Fields.Item("file_ext".Value = "PDF" Then %>
<img src="../files/pdf.gif">
<% End If %>
<% If rsDownloads.Fields.Item("file_ext".Value = "TXT" Then %>
<img src="../files/txt.gif">
<% End If %>
</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:
<%
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)
%>
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>
<img src="images/icons/<%=strExt %>.gif" width="18" height="18" align="absmiddle">
Works like a charm.
<b>Jeremy Conn</b>
www.conncreativemedia.com
DWMX2004 | ASP/VB | Access/SQL2000
This code produced the result that I want... take a look:
<%
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)
%>
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>
<img src="images/icons/<%=strExt %>.gif" width="18" height="18" align="absmiddle">
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>
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.
<%
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
%>
<!-- Replace file.extension with your database file name -->
<img src="images/<%=icon("file.extension"%>" />
<%
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
%>
<!-- Replace file.extension with your database file name -->
<img src="images/<%=icon("file.extension"%>" />