how to get the servername + foldername without containing the script name?
string filePath = Request.QueryString.Get("filepath");
string serverPath = Request.ServerVariables["SERVER_NAME"] + "/";
string fullUrl = "http://" + serverPath + filePath;
Response.Write(fullUrl);
the above code is missing folder name.
This page demonstrates how the parts of a HttpRequest break down. You could take the Request.FilePath and remove the final segment like so:
string directory = Request.FilePath.Remove(Request.FilePath.LastIndexOf('/'));
You can't quite do that, because HTTP is "blind" to what's a folder and what's a file.
You are probably familiar with URLs such as http://www.acme.com/products/view.asp but a completely valid URL could also be http://www.acme.com/products/view , so you can't differentiate a folder and a file name.
What you CAN do, provided that:
you know your application and know that it only runs files (as opposed to some routing mechanism that runs some logical piece of code, such as in MVC for example)
each file contains an extension
is to parse the path string on your own and look for file.ext pattern in the end of the path.
Related
In my asp.net application, I am writing a file in code behind. I then want to use this file as below into a Handler but I get an error 'Illegal characters in Path'. I can't understand why? Help please.
The value of files in below is "306963020170816111848_Generic_P.pdf" and the file definitely exists in the correct path
WebClient client = new WebClient();
client.DownloadString(#"Handlers/MyPrintPdf.ashx?PdfFile=" + Server.MapPath("~/Templates/MyFiles/" + files)); // error here
Changed to use
HttpUtility.UrlEncode(#"Handlers/MyPrintPdf.ashx?PdfFile=" + Server.MapPath("~/Templates/MyFiles/" + files));
You should use the Uri overload of your DownloadString method. These parameters must be url encoded.
EDIT:
HttpUtility.UrlEncode(url)should also work.
my need is to display an image in a web page using string builder. i know through this snippet sb.Append("<img src=\"bla.jpg\" />"); i can display an image. Here my problem is that the image path is dynamically generated and is in a string variable.
following is the code i tried:
Dim table_builder As New StringBuilder
table_builder.Append("<table width=100%>")
Dim filePath As String = Server.MapPath("../attachments/") & fileName
// file name is the name of the file fetching from the database
table_builder.Append("<tr>")
table_builder.Append("<td>")
table_builder.Append("<img src='" + filePath + "'/>")
table_builder.Append("</td>")
table_builder.Append("</tr>")
But it does not show the image through executing the code
table_builder.Append("</table>")
attach.innerHTML = table_builder.ToString()
Server.MapPath("../attachments/") will map the physical path, that's why the image is not showing. so you can edit your code as:
table_builder.Append("<table width=100%>")
table_builder.Append("<tr>")
table_builder.Append("<td>")
table_builder.Append("<img src='" + "../attachments/" & fileName + "'/>")
table_builder.Append("</td>")
table_builder.Append("</tr>")
table_builder.Append("</table>")
attach.innerHTML = table_builder.ToString()
For your reference ,The Answer by splattne says that :
Server.MapPath specifies the relative or virtual path to map to a physical directory.
Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
Server.MapPath("..") returns the parent directory
Server.MapPath("~") returns the physical path to the root of the application
Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
Server.MapPath will return the PHYSICAL path of the file on the server. You certainly don't want that as the file will not be accessible from the client side.
You should use relative path
like Dim filePath As String = ("/attachments/") & fileName
with that the file should be accessible from client as
http://domainname.com/attachments/filename
also be careful while using ../ in your paths. You might end up one level lower to your root path and it might not be accessible from client side
Typically ~ can be used to always start the paths from site root
I'm developing a ASP.NET MVC application, and I'm managing file names in my URL paths. I need to encode url file names, to avoid errors on some paths (for example, when the character '+' is used, it fails under Mono/XSP).
But I need to encode only the file names, for example:
For example, given this input:
/dir1/this has spaces/file+name.txt
I need the following output:
/dir1/this%20has%20spaces/file%2bname.txt
Note that I don't want to encode the path separator. Any suggestion?
Thanks in advance.
EDIT: I'm building my URL using the following:
<%= Html.ActionLink(
Html.Encode(revision.Name),
"Details",
"Items",
Model.GetRouteParameters(revision.FullPath),
null) %>
Where GetRouteParameters is the following:
public object GetRouteParameters(string path)
{
return new
{
repository = ItemProvider.GetBrowsingObject().Repository,
path = path,
browsing = ItemProvider.GetBrowsingObject().Name
};
}
I would like to encode any "dangerous" character in the repository, the path or the browsing names.
You can use HttpUtility.UrlEncode to encode the strings in a url safe way. You need to url encode the separate path components and then create the path.
I have image files stored in "VS_Project\Resources\Images".
When I use the following code:
String str = Server.MapPath("a.png");
str becomes "VS_Project\a.png".
Why isn't it returning the full path?
You need a / before the a.png. If Path starts with either a forward (/) or backward slash (\), the MapPath method returns a path as if Path were a full, virtual path. If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the .asp file being processed.
Try this:
string str = Server.MapPath("/") + "\\Resources\\Images\\a.png";
How do you get the name of the current virtual directory using ASP Classic? In ASP.NET you can use Request.ApplicationPath to find this.
For example, let's say you have a URL like this:
http://localhost/virtual_directory/subdirectory/file.asp
In ASP.NET, Request.ApplicationPath would return /virtual_directory
You can get the virtual path to the file from one of several server variables - try either:
Request.ServerVariables("PATH_INFO")
Request.ServerVariables("SCRIPT_NAME")
(but not INSTANCE_META_PATH as previously suggested - this gives you the meta base path, not the virtual path you're expecting).
Either server variable will give you the virtual path including any sub-directories and the file name - given your example, you'll get "/virtual_directory/subdirectory/file.asp". If you just want the virtual directory, you'll need to strip off everything after the second forward slash using whatever method you prefer for plucking a directory out of a path, such as:
s = Request.ServerVariables("SCRIPT_NAME")
i = InStr(2, s, "/")
If i > 0 Then
s = Left(s, i - 1)
End If
or:
s = "/" & Split(Request.ServerVariables("SCRIPT_NAME"), "/")(1)
Try using: Request.ServerVariables("SCRIPT_NAME")
or try using Request.ServerVariables("INSTANCE_META_PATH") if that does not work for you.
For a list of other server variables try this link:
http://www.w3schools.com/asp/coll_servervariables.asp