Link to home
Start Free TrialLog in
Avatar of Member_2_6492660_1
Member_2_6492660_1Flag for United States of America

asked on

My ASP coded Web PAge is Building incorrect URL HyperLinks

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

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

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.
Avatar of Member_2_6492660_1

ASKER

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
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.
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

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.
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?
Can you use mappath outside of the site?  I think you may need to use something like  "F:\music\mp3musicalbums\"&path
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
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...
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

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
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
Rainer,

ok done  check this out

http://www.tgcsnet.com/music_17.asp
ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany 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
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
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
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?
worked great

now onto tweaking