Getting Relative Virtual Path from Physical Path

Getting relative virtual path from physical path

Maybe this question is what you're looking for.
There they suggest:

String RelativePath = AbsolutePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);

How to convert physical path to virtual path

Something like the following should do the trick. A little more code than the previous answer, but you know, sometimes I like to do things the hard way.

    string path = @"C:\Users\AlphaDog\Desktop\Alumni Revised\AlumiTrackingSystem\AlumiTrackingSystem\AlumiTrackingSystem\AlumiTrackingSystem\image\Vince\Tulips.jpg";
string[] splitPath = path.Split('\\');
int start = 0;
foreach (string s in splitPath) {
if (s == "image")
break;
else
start++;
}
string virtualPath = "~/";
for (int i = start; start < splitPath.Length; start++) {
virtualPath += (i > start ? "/" : "") + splitPath[start];
}

How to determine if a relative path is outside of a virtual path

Server.MapPath is an easy way to validate either virtual or dos style paths. Validation is restricted for security reasons from validating any physical paths that are outside the web site. See my comment above.

Storing Physical Path on database instead of virtual one

Store the virtual path before you call Server.MapPath on it, then use that in your DB record.

var fileName = Path.GetFileName(file.FileName);
var combinedVirtualPath = Path.Combine("~/Content/uploads", fileName);
var phisicalPath = Server.MapPath(combinedVirtualPath);
file.SaveAs(phisicalPath);
DriverReg newRecord = new DriverReg();
newRecord.FullName = model.FullName;
newRecord.Address = model.Address;
newRecord.Postcode = model.Postcode;
newRecord.Contact = model.Contact;
newRecord.Email = model.Email;
newRecord.County = model.County;
newRecord.File = combinedVirtualPath;
newRecord.Date = DateTime.Now;

Converting physical path to relative one in respect of http://localhost:

The easiest thing to do is get rid of the physical application path.
If you cannot do that in your code, just strip it off the pathRaw variable. Like this:

public string GetVirtualPath( string physicalPath )
{
if ( !physicalPath.StartsWith( HttpContext.Current.Request.PhysicalApplicationPath ) )
{
throw new InvalidOperationException( "Physical path is not within the application root" );
}

return "~/" + physicalPath.Substring( HttpContext.Current.Request.PhysicalApplicationPath.Length )
.Replace( "\\", "/" );
}

The code first checks to see if the path is within the application root. If not, there's no way to figure out a url for the file so an exception is thrown.

The virtual path is constructed by stripping off the physical application path, convert all back-slashes to slashes and prefixing the path with "~/" to indicate it should be interpreted as relative to the application root.

After that you can convert the virtual path to a relative path for output to a browser using ResolveClientUrl(virtualPath).



Related Topics



Leave a reply



Submit