I have this function that gets the fileData as a byte array and a file path. The error I am getting is when it tries to set the fileInfo in the code bewlo. It says 'Physical Path given, Virtual Path expected'
public override void WriteBinaryStorage(byte[] fileData, string filePath)
{
try
{
// Create directory if not exists.
System.IO.FileInfo fileInfo = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(filePath)); //when it gets to this line the error is caught
if (!fileInfo.Directory.Exists)
{
fileInfo.Directory.Create();
}
// Write the binary content.
System.IO.File.WriteAllBytes(System.Web.HttpContext.Current.Server.MapPath(filePath), fileData);
}
catch (Exception)
{
throw;
}
}
When debugging it, is providing the filePath as "E:\\WEBS\\webapp\\default\\images\\mains\\myimage.jpg"
. And the error message is
'E:/WEBS/webapp/default/images/mains/myimage.jpg' is a physical path, but a virtual path was expected.
Also, what it is triggering this to happen is the following call
properties.ResizeImage(imageName, Configurations.ConfigSettings.MaxImageSize, Server.MapPath(Configurations.EnvironmentConfig.LargeImagePath));
Generally this physical and virtual path problem occurred whenever we refer “Server. MapPath” value multiple times while using folder path in applications. To solve this e:is a physical path but a virtual path was expected we need to use Server.
First of all, let's get the overview of both. Physical path - This is the actual path the file is located by IIS. Virtual path - This is the logical path to access the file which is pointed to from outside of the IIS application folder.
The virtual path of a web folder is (almost) never the same as the physical folder. In your code you will, reference both the physical path and the virtual path, depending on what you are coding. ASP.NET has 3 tools for working with folder paths: the ~ operator, the Server. MapPath method, and the Href method.
A virtual path (VP) is a collection of virtual channel connections (VC) made through an ATM network. Each VP or VC can be either permanently established or set up dynamically for the time needed to transmit information through the network.
If you already have a physical path, it doesn't make sense to call Server.MapPath
.
You're calling MapPath
twice.
Working:
string[] filesPath = Directory.GetFiles(Server.MapPath("~/txtPath/"));
foreach (string path in filesPath)
{
FileInfo fi = new FileInfo(path); //This Is Working
string LastAcceTime = fi.LastWriteTime; //Return Correct Value
}
Not Working:
string[] filesPath = Directory.GetFiles(Server.MapPath("~/txtPath/"));
foreach (string path in filesPath)
{
FileInfo fi = new FileInfo(Server.MapPath(path)); //This Is Worng
string LastAcceTime = fi.LastWriteTime; //Return 1/1/1601
}
Dont use Server.Mappath
twice
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With