Solved

My ASP coded Web PAge is Building incorrect URL HyperLinks

Posted on 2013-12-10
20
557 Views
Last Modified: 2013-12-11
Windows 2003 Server IIS6 ASP and ASP.NET

The code below works presents a web page that lists all the files in the mappath statement.

My problem is that the code is building an incorrect URL statement for each file.
So when someone clicks on the filename they get Error 404 page not found

This works...   typed in manually
http://www.tgcsnet.com/mp3/MP3MusicAlbums/Deep%20Purple/Machine%20Head/01%20-%20Highway%20Star%20%28LP%20Version%29.mp3


This is what is being built by my code.
http://www.tgcsnet.com/lbums/Deep%20Purple/Machine%20Head/01%20-%20Highway%20Star%20(LP%20Version).mp3

I by no means is an expert in ASP coding can use all the help I can get.


<% ListFolderContents(Server.MapPath("/mp3/mp3 music albums")) %>
<% sub ListFolderContents(path)
     dim fs, folder, file, item, url
     set fs = CreateObject("Scripting.FileSystemObject")
     set folder = fs.GetFolder(path)
    'Display the target folder and info.
     Response.Write("<h2>"& folder.Name &"</h2>")
     'Display a list of sub folders.
     for each item in folder.SubFolders
                ListFolderContents(item.Path)
     next
     'Display a list of files.
Response.Write("<ul>")
    for each item in folder.Files
       url = MapURL(item.path)
        Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
    next
        Response.Write("</ul>")
   end sub
   function MapURL(path)
     dim rootPath, url
     'Convert a physical file path to a URL for hypertext links.
     rootPath = Server.MapPath("/")
     url = Right(path, Len(path) - Len(rootPath))
     MapURL = Replace(url, "\", "/")
end function  %>

Open in new window

0
Comment
Question by:Thomas Grassi
  • 10
  • 5
  • 3
20 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
Making this readable

http://www.tgcsnet.com/mp3/MP3MusicAlbums/Deep Purple/Machine Head/01 - Highway Star (LP Versionz).mp3
http://www.tgcsnet.com/lbums/Deep Purple/Machine Head/01 - Highway Star (LP Version).mp3

Open in new window


Can you see your function where you are taking the right(path,len(path)-len(rootPath)) is probably throwing this off

http://www.tgcsnet.com/mp3/MP3MusicAlbums/Deep Purple/Machine Head/01 - Highway Star (LP Versionz).mp3
http://www.tgcsnet.com/              lbums/Deep Purple/Machine Head/01 - Highway Star (LP Version).mp3

Open in new window


If all mp3's are in the same folder, just use the actual location and file name.  It seems like MapPath is throwing you off.
0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
thanks for responding

No my MP3 files are not in the same folder

I have hundreds of folders and sub folders.

As I said above I am not an expert on ASP code.

Any ideas on what is wrong can you provide me a correction to my code?

Thanks
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
Hi,
the problem is that your function MapURL will probably always return the wrong shortened string.

Can you perhaps provide the info:
Where is your root folder of the web site (the full physical path)?
Where is your MP3 root folder (full physical path)?

You call the first function with
Server.MapPath("/mp3/mp3 music albums")

Open in new window

but then inside the mapUrl function you use
Server.MapPath("/")

Open in new window


using the substring RIGHT function which is causing this issue.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
See if this works

<% sub ListFolderContents(path)
    '***********************************************
    '   At this point, we know the path, we just need the file names in the path
    '***********************************************
     dim fs, folder, file, item, url
     set fs = CreateObject("Scripting.FileSystemObject")
     

     set folder = fs.GetFolder(path)

    'Display the target folder and info.
     Response.Write("<h2>"& folder.Name &"</h2>")
     'Display a list of sub folders.
     for each item in folder.SubFolders
                ListFolderContents(item.Path)
     next
     'Display a list of files.
Response.Write("<ul>")
    for each item in folder.Files
       ' url = MapURL(item.path)  **** take this line out and take out "url" below and use "path"
        Response.Write("<li><a href=""" & path & """>" & item.Name & "</a></li>")
    next
        Response.Write("</ul>")
   end sub
  %>

Open in new window

0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
Rainerj:

My Root Site this is where music_9.asp resides

c:\inetpub\wwwroot

My MP3 files reside here

F:\music\mp3musicalbums

I created an alias in IIS named MP3 that's why my mappath is /mp3/mpsmusic/albums.
0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
Padas

tried your code got a blank page.

did not find any files

is this complete code or do I need to replace some of mine?
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
Can you use mappath outside of the site?  I think you may need to use something like  "F:\music\mp3musicalbums\"&path
0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
Padas

<% ListFolderContents(Server.MapPath("/mp3/mp3musicalbums")) %>
<% sub ListFolderContents(path)
    '***********************************************
    '   At this point, we know the path, we just need the file names in the path
    '***********************************************
     dim fs, folder, file, item, url
     set fs = CreateObject("Scripting.FileSystemObject")
     

     set folder = fs.GetFolder(path)

    'Display the target folder and info.
     Response.Write("<h2>"& folder.Name &"</h2>")
     'Display a list of sub folders.
     for each item in folder.SubFolders
                ListFolderContents(item.Path)
     next
     'Display a list of files.
Response.Write("<ul>")
    for each item in folder.Files
       ' url = MapURL(item.path)  **** take this line out and take out "url" below and use "path"
        Response.Write("<li><a href=""" & path & """>" & item.Name & "</a></li>")
    next
        Response.Write("</ul>")
   end sub
  %>

Open in new window


check it out

http://www.tgcsnet.com/music_15.asp

it changed the url to file:///.........................

When you click on the name it does not do any thing.

I added the
<% ListFolderContents(Server.MapPath("/mp3/mp3musicalbums")) %>
to your code example

The idea here is to click on the file and play the song.

I do not think I can access them using F:\music\mp3musicalbums


Example please
0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
Rainerj:

Do not understand your last comment?

Did you mean to post new code?

Did you see my response to you I answered your questions?

Any ideas how I can get this to work so close...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
Hi,

could you try this?
<% ListFolderContents(Server.MapPath("/mp3/mp3 music albums")) %>
<% sub ListFolderContents(path)
     dim fs, folder, file, item, url
     set fs = CreateObject("Scripting.FileSystemObject")
     set folder = fs.GetFolder(path)
    'Display the target folder and info.
     Response.Write("<h2>"& folder.Name &"</h2>")
     'Display a list of sub folders.
     for each item in folder.SubFolders
                ListFolderContents(item.Path)
     next
     'Display a list of files.
Response.Write("<ul>")
    for each item in folder.Files
       url = MapURL(item.path)
        Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
    next
        Response.Write("</ul>")
   end sub
   function MapURL(path)
     dim rootPath, url
     'Convert a physical file path to a URL for hypertext links.
     rootPath = Server.MapPath("/")
     url = Right(path, Len(path) - 5)
     MapURL = Replace(url, "\", "/")
end function  %>

Open in new window

0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
Rainerj.

Ok thanks I need to know how to do that for future postings.

I tested your code we are real close now

here is what is built

http://www.tgcsnet.com/sic/MP3MusicAlbums/Deep%20Purple/Machine%20Head/01%20-%20Highway%20Star%20(LP%20Version).mp3

SIC ????  needs to be mp3
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
Hi,

sorry I have no IIS6 server at hand, therefore could you please try this:
<% ListFolderContents(Server.MapPath("/mp3/mp3 music albums")) %>
<% sub ListFolderContents(path)
     dim fs, folder, file, item, url
     set fs = CreateObject("Scripting.FileSystemObject")
     set folder = fs.GetFolder(path)
    'Display the target folder and info.
     Response.Write("<h2>"& folder.Name &"</h2>")
     'Display a list of sub folders.
     for each item in folder.SubFolders
                ListFolderContents(item.Path)
     next
     'Display a list of files.
Response.Write("<ul>")
    for each item in folder.Files
       url = MapURL(item.path)
        Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
    next
        Response.Write("</ul>")
   end sub
   function MapURL(path)
     dim rootPath, url
     'Convert a physical file path to a URL for hypertext links.
     rootPath = Server.MapPath("/")
	 Response.Write("<li>Rootpath: -" & rootPath & "- | path: -"& path & "-</li>")
     url = Right(path, Len(path) - 5)
     MapURL = Replace(url, "\", "/")
end function  %>

Open in new window

This will dump the two important variables and then we should be able to get an idea about where and how this paths come from.

HTH
Rainer
0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
Rainer,

ok done  check this out

http://www.tgcsnet.com/music_17.asp
0
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
Comment Utility
Hi,
then this should do the trick:
<% ListFolderContents(Server.MapPath("/mp3/mp3 music albums")) %>
<% sub ListFolderContents(path)
     dim fs, folder, file, item, url
     set fs = CreateObject("Scripting.FileSystemObject")
     set folder = fs.GetFolder(path)
    'Display the target folder and info.
     Response.Write("<h2>"& folder.Name &"</h2>")
     'Display a list of sub folders.
     for each item in folder.SubFolders
                ListFolderContents(item.Path)
     next
     'Display a list of files.
Response.Write("<ul>")
    for each item in folder.Files
       url = MapURL(item.path)
        Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
    next
        Response.Write("</ul>")
   end sub
   function MapURL(path)
     dim rootPath, url
     'Convert a physical file path to a URL for hypertext links.
     url = "/mp3/" & Right(path, Len(path) - 9)
     MapURL = Replace(url, "\", "/")
end function  %>

Open in new window

0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
Rainerj:

Thanks that works.

Is there anyway I can stop someone from download the file?

I just want them to be able to play the song not download it
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
Hi,
afaik this would not be that easy (if not even impossible). You would need some kind of streaming server and even with that there are tools/programs available to save the streams.

HTH
Rainer
0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
Raier

Ok sounds like a lot of work in that
was just wondering how amazon and others do that.

Will look into that in the future.

One more question for you.

Is there a way to only display .mp3 .m4a and .wma file types?
don't really need to display the art work and other files in this listing?
0
 
LVL 23

Author Closing Comment

by:Thomas Grassi
Comment Utility
worked great

now onto tweaking
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now