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
C# Httpwebrequest the Underlying Connection Was Closed: an Unexpected Error Occurred on a Send
Find If Point Lies on Line Segment
Convert Object to JSON String in C#
Serialization Breaks in .Net 4.5
Changing Read Only Properties with Reflection
How to Implement One "Catch'Em All" Exception Handler with Resume
What Does "Displayclass" Name Mean When Calling Lambda
Top Level Domain from Url in C#
C# - How to Detect a Windows Shutdown/Logoff and Cancel That Action (After Asking the User)
Convert String to Brushes/Brush Color Name in C#
How to Simply 'Read' a File That Is in Use
How to Make Correct Date Format When Writing Data to Excel
C# Controlling a Transaction Across Multiple Databases
How to Get an Extension Method to Change the Original Object
Image.Fromstream() Method Returns Invalid Argument Exception