Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Root dir to dynamic MapPath 1

Status
Not open for further replies.

cumap

IS-IT--Management
Jul 9, 2007
268
US
Hello all,
I'm not a dotnet user and I'm having a problem changing a root file location to MapPath type from this code I found
Code:
<%@ Import Namespace="System.IO"%>
<html>
<head>
<title>File Download</title>
<script language="VB" runat="server">


Sub Page_Load(sender As Object, e As EventArgs)
 
  Dim root As String = "C:\temp\"
 
  Dim files() As String = Directory.GetFiles(root)
  Dim sb As New StringBuilder(2048)
  Dim f As String
  For Each f In files
    Dim filename As String = Path.GetFileName(f)
    sb.Append("<br><a href=FileDownload.aspx?file=")
    sb.Append(root).Append(Server.UrlEncode(filename))
    sb.Append(">").Append(filename).Append("</a>")
  Next
  fileList.Text = sb.ToString()

End Sub

</script>
</head>
<body>
<form runat="server">
<asp:Label id="fileList" runat="server"/>
</form>
</body>
</html>

I don't know where to edit/change the C:// to something more dynamic since I'll need this for server host purposes.

Thanks!
 
if the file is within the virtual directory use Server.MapPath("relative file path");

if the file is outside the virtual directory (which it really should not be) you will need to explicitly provide the absolute path through hardcoding or configuration setting. You will also need to consider the user context in which IIS is running. it will need permissions to the remote file.
By remote I mean not within the virtual directory.

also you really should put the code behind in a separate file. Mixing the server code with markup is much more difficult to maintain.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Well, the reason is that I have a website hosted by Godaddy and I have no idea their root directory is. So the script on the above is no good since it located the root C: dir as default example with backslashes. Now, I'd like to change all that to something more dynamic so that I don't need to know what is my godaddy's root dir by utilizing MapPath, but by examine the DOTNET code, I stuck and don't know where to change to my requirement.

Help!
 
assuming you create a Downloads directory off of root within your virtual directory.
Code:
var pathToDownload = Server.MapPath("~/Downloads");
var directory = new DirectoryInfo(pathToDownload);
foreach(var file in directory.GetFiles())
{
   //do something with each file
}
You can also get the absolute path
Code:
HttpContext.Current.Request.Uri.AbsolutePath
or something like that.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
So judging by the above script, should I change at this location
Code:
Dim root As String = Server.MapPath("~/temp");

or here,
Code:
sb.Append(Server.MapPath(root)).Append(Server.UrlEncode(filename))
 
if temp is a subdirectory off of the root then yes.

Also, the way you are going about creating list of links will not work. it will produce text, not links.

you should never have to build html in the code behind. just bind the array of filenames to a repeater. something like
Code:
public void Page_Load(object sender, EventArgs e)
{
  if(IsPostBack) return;
  var path = Server.MapPath("~/temp");
  var files = Directory.GetFiles(path);
  MyRepeater.DataSource = files;
  DataBind();
}
Code:
<asp:Repeater ID="MyRepeater" runat="server">
   <SeparatorTemplate>
      <br/>
   </SeparatorTemplate>
   <ItemTemplate>
      <a target="_blank" href='FileDownload.ashx?file=<%#Eval(Container.DataItem)%>'><%#Eval(Container.DataItem)%></a>
   </ItemTemplate>
</asp:Repeater>
something like that. since you are downloading the file you can either link directly to the file (not need to render html for that). or you can send them to a generic handler (ASHX extension). This allows you to do some pre/post processing.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Perfect! Thank you for the help Jason. The MapPath you given is working as I wanted.

Peace!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top